rust converting from u32 to string and vice versa
Converting to int to string and vice versa can be challenging especially for beginner like me. The following code demonstrate how to convert back and forth.
fn main() {
// u32 to string
let a = 32;
let s = a.to_string();
println!("{}", s);
// string to u32
let a = "32";
let b: u32 = a.parse().unwrap();
}
Notice how vscode infer the type here
u can see a.to_string() returns a string - which are stored in heap. Converting str to u32 can be achieve by calling parse().unwrap(). unwrap() returns a Result<T, E> which gives results and error.
Comments