如何从字符串解析i64?

时间:2015-02-01 09:31:25

标签: rust

显然,某些事情发生了变化,因此我无法从字符串中解析i64:

use std::from_str::FromStr;

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

错误:

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)

1 个答案:

答案 0 :(得分:10)

导入失败,因为FromStr特征现在是std::str::FromStr。此外,from_str已不在prelude中。将字符串转换为整数的首选方法是str::parse

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

打印

Some(123)

Demo