Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse i64 from a string? [duplicate]

Tags:

rust

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)
like image 806
Incerteza Avatar asked Feb 01 '15 09:02

Incerteza


1 Answers

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

like image 98
Dogbert Avatar answered Sep 18 '22 04:09

Dogbert