Skip to content

Instantly share code, notes, and snippets.

@jimmychu0807
Created November 21, 2019 10:20
Star You must be signed in to star a gist
Embed
What would you like to do?
Conversion between String, str, Vec<u8>, Vec<char> in Rust
use std::str;
fn main() {
// -- FROM: vec of chars --
let src1: Vec<char> = vec!['j','{','"','i','m','m','y','"','}'];
// to String
let string1: String = src1.iter().collect::<String>();
// to str
let str1: &str = &src1.iter().collect::<String>();
// to vec of byte
let byte1: Vec<u8> = src1.iter().map(|c| *c as u8).collect::<Vec<_>>();
println!("Vec<char>:{:?} | String:{:?}, str:{:?}, Vec<u8>:{:?}", src1, string1, str1, byte1);
// -- FROM: vec of bytes --
// in rust, this is a slice
// b - byte, r - raw string, br - byte of raw string
let src2: Vec<u8> = br#"e{"ddie"}"#.to_vec();
// to String
// from_utf8 consume the vector of bytes
let string2: String = String::from_utf8(src2.clone()).unwrap();
// to str
let str2: &str = str::from_utf8(&src2).unwrap();
// to vec of chars
let char2: Vec<char> = src2.iter().map(|b| *b as char).collect::<Vec<_>>();
println!("Vec<u8>:{:?} | String:{:?}, str:{:?}, Vec<char>:{:?}", src2, string2, str2, char2);
// -- FROM: String --
let src3: String = String::from(r#"o{"livia"}"#);
let str3: &str = &src3;
let char3: Vec<char> = src3.chars().collect::<Vec<_>>();
let byte3: Vec<u8> = src3.as_bytes().to_vec();
println!("String:{:?} | str:{:?}, Vec<char>:{:?}, Vec<u8>:{:?}", src3, str3, char3, byte3);
// -- FROM: str --
let src4: &str = r#"g{'race'}"#;
let string4 = String::from(src4);
let char4: Vec<char> = src4.chars().collect();
let byte4: Vec<u8> = src4.as_bytes().to_vec();
println!("str:{:?} | String:{:?}, Vec<char>:{:?}, Vec<u8>:{:?}", src4, string4, char4, byte4);
}
@kevinjanada
Copy link

Thank you for this

@hvina
Copy link

hvina commented May 5, 2021

Very useful. Thanks

@mugendi
Copy link

mugendi commented May 14, 2021

Couldn't tank you enough?

@txrx0031
Copy link

Thanks!

@autoprog
Copy link

autoprog commented Aug 8, 2021

obrigado!

@NotRoland
Copy link

I keep using this as a reference, lol. Thank you!

@developersuper
Copy link

Thank you so much.
This helped me a lot

@Stef16Robbe
Copy link

Thank you so much, saved a long night's work!

@greister
Copy link

greister commented May 4, 2022

very useful, Thank you !

@spdrman
Copy link

spdrman commented Jul 1, 2022

Brilliant!

@MFQWKMR4
Copy link

MFQWKMR4 commented Jul 2, 2022

arigato

@alsiesta
Copy link

Great. Thank you.

@lemoncmd
Copy link

lemoncmd commented Sep 24, 2022

@jimmychu0807 Line 11 & 24: multibyte characters are not supported?

@MarcoDiMarek
Copy link

Lovely. Thanks.

@Patroklos99
Copy link

hello, based department?

@18601673727
Copy link

My favourite about Rust would be transforming String using let char3: Vec<char> = src3.chars().collect::<Vec<_>>(); into Vec<char>, it makes so much sense!

@bguo068
Copy link

bguo068 commented May 16, 2023

Just found that an alternative way of converting String to Vec is this String::into_bytes().

pub fn into_bytes(self) -> Vec<u8, Global>

Converts a String into a byte vector.

This consumes the String, so we do not need to copy its contents.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment