How to disassemble a float with night rust?

I cannot learn how to parse a float from the current Rust, according to the documentation. I think this should work:

use std::f32;
use std::from_str::FromStr;

fn main() {
    let result = f32::from_str("3.14");
    println!("{}", result.unwrap());
}

but the compiler complains:

<anon>:5:18: 5:31 error: unresolved name `f32::from_str`.
<anon>:5     let result = f32::from_str("3.14");
                          ^~~~~~~~~~~~~

(See the Rusty Manege section: here )

What am I missing here?

+4
source share
2 answers

Currently, methods of static features can only be called through a feature, and the return value is displayed through type inference, therefore let x: Option<f32> = FromStr::from_str("3.14");. This will be more flexible if UFCS is implemented ( # 16293 ), at least eliminating the need to write a full type signature Option<f32>.

from_str , , . , , :

fn main() {
    let result = from_str::<f32>("3.14");
    println!("{}", result.unwrap());
}

playpen

+7

1.0.0 alpha ~ Nightly std:: str:: StrExt:: parse

assert_eq!("3.14".parse(), Ok(3.14f64))

fn main() {
    let result: f32 = "3.14".parse().unwrap();
    println!("{}", result);
}

+9

All Articles