Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Convert string to float

  • f32

  • Sometimes we are expecting floating point numbers.

fn main() {
    let text = " 2.3 ";
    println!("'{}'", text);
    let number: f32 = text.trim().parse().expect("Could not convert to f32");

    println!("'{}'", number);
    println!("'{}'", number + 1.0);
}
' 2.3 '
'2.3'
'3.3'