Q:

Rust program to convert a string into float

belongs to collection: Rust Basic Programs

0

In this program, we will convert a string into float using the parse() and unwrap() function. After that, we printed the result.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to convert a string into the float is given below. The given program is compiled and executed successfully.

// Rust program to convert a string 
// into float

fn main() 
{
    let mut strVar = "123.45";
    let mut floatVar:f32 = 0.0;
    
    floatVar=strVar.parse().unwrap();
    println!("Number is : {}",floatVar);
}

Output:

Number is : 123.45

Explanation:

Here, we created a variable strVar of &str type. Then we assigned the value of strVar variable into f32 variable using parse() and unwrap() functions. After that, we printed the result.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Rust Basic Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Rust program to convert an integer into a string... >>
<< Rust program to convert a string into an integer...