string - Converting a str to a &[u8] -
this seems trivial, cannot find way it.
for example,
fn f(s: &[u8]) {} pub fn main() { let x = "a"; f(x) }
fails compile with:
error: mismatched types: expected `&[u8]`, found `&str` (expected slice, found str) [e0308]
documentation, however, states that:
the actual representation of strs have direct mappings slices: &str same &[u8].
you can use as_bytes method:
fn f(s: &[u8]) {} pub fn main() { let x = "a"; f(x.as_bytes()) }
or, in specific example, use byte literal:
let x = b"a"; f(x)
Comments
Post a Comment