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

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -