from_bytes
fn from_bytes(Vec<byte> bytes) -> Result<string, IoError> Import
Source: use string::{from_bytes}; 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#
| Name | Type |
|---|---|
bytes | Vec<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_bytesbyte_at: Returns the byte at byte offsetiofs, or-1wheniis out of range.find_from: Returns the byte offset of the firstneedleinhayat or afterstart, or-1.format: Builds a string from the literalfmtand the arguments that follow it.match_at: Returns whetherneedleoccurs insstarting exactly at byte offsetat.rfind: Returns the byte offset of the lastneedleinhay, or-1.slice_bytes: Returns the part ofsbetween byte offsetsstart(inclusive) andend(exclusive).