Note: this question contains deprecated pre-1.0 code! The answer is correct, though.
To convert a str
to an int
in Rust, I can do this:
let my_int = from_str::<int>(my_str);
The only way I know how to convert a String
to an int
is to get a slice of it and then use from_str
on it like so:
let my_int = from_str::<int>(my_string.as_slice());
Is there a way to directly convert a String
to an int
?
use std::str::FromStr;
fixes that. More on from_str if you'd like. doc.rust-lang.org/std/str/trait.FromStr.html#tymethod.from_str – cryptograthor