ADVERTISEMENT
ADVERTISEMENT

Rust program to convert a string into an integer

Rust | Convert a string into an integer: Given a string, we have to convert it into an integer.
Submitted by Nidhi, on October 08, 2021

Problem Solution:

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

Program/Source Code:

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

// Rust program to convert a 
// string into an integer

fn main() 
{
    let mut strVar = "123";
    let mut intVar:i8 = 0;
    
    intVar=strVar.parse().unwrap();
    println!("Number is : {}",intVar);
}

Output:

Number is : 123

Explanation:

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

Rust Basic Programs »



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.