Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a float with nightly Rust?

Tags:

rust

I can't find out how to parse a float from 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 on Rust playpen : here)

What I am missing here ?

like image 669
Levans Avatar asked Dec 02 '22 15:12

Levans


1 Answers

In 1.0.0 alpha~Nightly you can use std::str::StrExt::parse instead

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

Also here's a sample with your code

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

Playpen link

like image 148
AbdullahDiaa Avatar answered Dec 15 '22 02:12

AbdullahDiaa