Function in string compiler

to_bytes

fn to_bytes(string s) -> Vec<byte>
Import
use string::{to_bytes};
Source: compiler/src/builtin_docs/string.hy

Encodes s as UTF-8 into a new Vec<byte>.

Always succeeds; the result has s.len() bytes. Use it to pass text to io::write and the other byte-based IO functions.

Parameters#

NameType
sstring

Returns#

Vec<byte>

Examples#

use string::{to_bytes};
use tests::assert;
let b = to_bytes("Hi");
assert!(b.len() == 2);
assert!(b[0] == 72 as byte && b[1] == 105 as byte);

See also#

  • Vec
  • io::write
  • 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.
  • from_bytes: Decodes bytes as UTF-8 text.
  • 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.
↑ ↓ navigate ↵ open