From the course: Rust Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Parse strings

Parse strings - Rust Tutorial

From the course: Rust Essential Training

Start my 1-month free trial

Parse strings

- [Instructor] The data we get from standard input is a string, but if we need to interpret and use that input information as a different data type, we'll need to parse it. For example, let's say we want the user to enter a number. If we want to work with that input as a numeric value, we'll need to convert the input string into the corresponding integer value it represents. To do that, let's declare a new variable to hold that value. And then we'll parse the string buffer to get it. The first thing we'll do is apply the trim method to remove any leading or trailing white spaces from the string. And this is necessary because the string we get from standard input will include a new line character at the end, so we need to get rid of that before we try to parse it. Next, we'll use the parse method on that trimmed string. Now, parse is a general method that can parse strings into a lot of different data types, and it's not clear…

Contents