18

Apparently, something has changed and thus I can't parse i64 from string:

use std::from_str::FromStr;

let tree1: BTreeMap<String, String> = //....
let my_i64: i64 = from_str(tree1.get("key1").unwrap().as_slice()).unwrap();

Error:

16:27 error: unresolved import `std::from_str::FromStr`. Could not find `from_str` in `std`

$ rustc -V
rustc 1.0.0-nightly (4be79d6ac 2015-01-23 16:08:14 +0000)
0

1 Answer 1

45

Your import fails because the FromStr trait is now std::str::FromStr. Also, from_str is no longer in the prelude. The preferred way to convert strings to integers is str::parse

fn main() {
    let i = "123".parse::<i64>();
    println!("{:?}", i);
}

prints

Ok(123)

Demo

Sign up to request clarification or add additional context in comments.

5 Comments

Use let i: i64 = "123".parse(); instead of ugly template syntax if possible.
@hauleth, .parse() returns Option<T>, so that would have to be let i: Option<i64> = "123".parse();.
Yes. Of course. I've forgotten.
update: nowadays it returns Result<F, <F as FromStr>::Err> so simple .parse() won't work, or am I wrong?
@KeeperHood A simple parse will still work; you just need to handle the error like you used to handle the Option.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.