Created
June 23, 2020 18:09
-
-
Save johnptoohey/6a7e7b37711f30d4eebcc54dacf473d2 to your computer and use it in GitHub Desktop.
Rust - Conversions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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