Function in prelude auto-imported

ord

fn ord(string s) -> Result<byte, string>
Source: compiler/src/builtin_docs/prelude.hy

Returns the code of the single character in s as a byte.

s must hold exactly one character whose Unicode code point is at most 255 (ASCII or Latin-1, so ord("é") is 233). Otherwise the result is Err with a message: "empty string", "expected single character" or "character code out of byte range". The inverse is char.

Parameters#

NameType
sstring

Returns#

Result<byte, string>

Examples#

use tests::assert;
let code = match ord("A") {
    Result::Ok(b) => b as int,
    Result::Err(_) => -1,
};
assert!(code == 65);
let err = match ord("AB") {
    Result::Ok(_) => "",
    Result::Err(e) => e,
};
assert!(err == "expected single character");

See also#

  • byte
  • char
  • black_box: Returns x unchanged, through a call the optimizer cannot see into.
↑ ↓ navigate ↵ open