Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save johnptoohey/6a7e7b37711f30d4eebcc54dacf473d2 to your computer and use it in GitHub Desktop.

Select an option

Save johnptoohey/6a7e7b37711f30d4eebcc54dacf473d2 to your computer and use it in GitHub Desktop.
Rust - Conversions
# Rust type conversions String, &str, Vec<u8> and &[u8]
```rust
let s:String = ...
let st:&str = ...
let u:&[u8] = ...
let v:Vec<u8> = ...
&str -> String String::from(st)
&str -> &[u8] st.as_bytes()
&str -> Vec<u8> st.as_bytes().to_owned() via &[u8]
String -> &str &s alt. s.as_str()
String -> &[u8] s.as_bytes()
String -> Vec<u8> s.into_bytes()
&[u8] -> &str str::from_utf8(u).unwrap()
&[u8] -> String String::from_utf8(u).unwrap()
&[u8] -> Vec<u8> u.to_owned()
Vec<u8> -> &str str::from_utf8(&v).unwrap() via &[u8]
Vec<u8> -> String String::from_utf8(v)
Vec<u8> -> &[u8] &v
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment