Function in string compiler

from_bytes

fn from_bytes(Vec<byte> bytes) -> Result<string, IoError>
Import
use string::{from_bytes};
Source: compiler/src/builtin_docs/string.hy

Decodes bytes as UTF-8 text.

Returns Err(IoError::InvalidInput) when bytes is not valid UTF-8. The inverse of to_bytes.

Parameters#

NameType
bytesVec<byte>

Returns#

Result<string, IoError>

Examples#

use io::{IoError};
use string::{from_bytes};
use tests::assert;
let text = match from_bytes(Vec::from([111 as byte, 107 as byte])) {
    Result::Ok(s) => s,
    Result::Err(_) => "",
};
assert!(text == "ok");
let bad = match from_bytes(Vec::from([255 as byte])) {
    Result::Err(IoError::InvalidInput) => true,
    default => false,
};
assert!(bad);

See also#

  • string::to_bytes
  • byte_at: Returns the byte at byte offset i of s, or -1 when i is out of range.
  • find_from: Returns the byte offset of the first needle in hay at or after start, or -1.
  • format: Builds a string from the literal fmt and the arguments that follow it.
  • match_at: Returns whether needle occurs in s starting exactly at byte offset at.
  • rfind: Returns the byte offset of the last needle in hay, or -1.
  • slice_bytes: Returns the part of s between byte offsets start (inclusive) and end (exclusive).
↑ ↓ navigate ↵ open