0

I've been trying to find an easy way to read variables in Rust, but haven't had any luck so far. All the examples in the Rust Book deal with strings AFAIK, I couldn't find anything concerning integers or floats that would work.

Nicolás Siplis
  • 71
  • 3
  • 16
  • [The Rust Programming Language, chapter 3.1 — Guessing game](http://doc.rust-lang.org/stable/book/guessing-game.html): *The `parse()` method on strings parses a string into some kind of number* – Shepmaster Jul 05 '15 at 22:29

3 Answers3

5

I don't have a Rust compiler on this machine, but based in part on this answer that comes close, you want something like...

let user_val = match input_string.parse::<i32>() {
    Ok(x) => x,
    Err(_) => -1,
};

Or, as pointed out in the comments,

let user_val = input_string.parse::<i32>().unwrap_or(-1);

...though your choice in integer size and default value might obviously be different, and you don't always need that type qualifier (::<i32>) for parse() where the type can be inferred from the assignment.

Community
  • 1
  • 1
John C
  • 1,931
  • 1
  • 22
  • 34
  • 2
    You don't need a compiler installed for things like this - the [Rust playpen](https://play.rust-lang.org/) has you covered. – Shepmaster Jul 05 '15 at 22:29
  • 1
    Using [`unwrap_or`](http://doc.rust-lang.org/std/result/enum.Result.html#method.unwrap_or) would be more idiomatic here: `input_string.parse::().unwrap_or(-1)`. You could also us this to specify the integer type: `input_string.parse().unwrap_or(-1i32)`. – Shepmaster Jul 05 '15 at 22:32
  • @Shepmaster, both very interesting, thanks! `unwrap_or` seems less idiomatic to me, but if that's the way the community is swinging, it works for me. – John C Jul 06 '15 at 10:17
4

To read user input, you always read a set of bytes. Sometimes, you can interpret those bytes as a UTF-8 string. You can then further interpret the string as an integral or floating point number (or lots of other things, like an IP address).

Here's a complete example of reading a single line of input and parsing it as a 32-bit signed integer:

use std::io;

fn main() {
    let mut input = String::new();
    io::stdin().read_line(&mut input).expect("Not a valid string");
    let input_num: i32 = input.trim().parse().expect("Not a valid number");
    println!("Your number plus one is {}", input_num + 1);
}

Note that no user-friendly error handling is taking place. The program simply panics if reading input or parsing fails. Running the program produces:

$ ./input
41
Your number plus one is 42
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
0

A set of bytes comprises an input. In Rust, you accept the input as a UTF-8 String. Then you parse the string to an integer or floating point number. In simple ways you accept the string and parse it, then write an expect`` statement for both, to display a message to the user what went wrong when the program panics during runtime.

fn main() {
    let mut x = String::new();
    std::io::stdin().read_line(&mut x)
        .expect("Failed to read input.");
    let x: u32 = x.trim().parse()
        .expect("Enter a number not a string.");
    println!("{:?}", x);
}

If the program fails to parse the input string then it panics and displays an error message. Notice that the program still panics and we are not handling an error perfectly. One more thing to notice is that we can use the same variable name x and not some x_int because of the variable shadowing feature. To handle the error better we can use the match construct.

fn main() {
    let mut x = String::new();
    match std::io::stdin().read_line(&mut x) {
        Ok(_) => println!("String has been taken in."),
        Err(_) => {
            println!("Failed to read input.");
            return;
        },
    };
    let x: u32 = match x.trim().parse() {
        Ok(n) => {
            println!("Converted string to int.");
            n
        },
        Err(_) => {
            println!("Failed to parse.");
            return;
        },
    };
    println!("{:?}", x);
}

This is longer way but a nicer way to handle errors and input and parse a number.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366