I'm trying to learn Rust by following the official guide.
However, I received an error regarding a String
not implementing the parse
method. I've searched the docs and found the method here.
For my peace of mind, I would like to know if there's an issue with the guide (quite likely for pre 1.0 language) or if I'm doing something wrong.
Rust version:
rustc 0.13.0-nightly (34d680009 2014-12-22 00:12:47 +0000)
I've copied the code and error bellow.
use std::io;
use std::rand;
fn main() {
println!("Guess the number!");
let secret_number = (rand::random::<uint>() % 100u) + 1u;
println!("The secret number is: {}", secret_number);
println!("Please input your guess.");
let input = io::stdin().read_line()
.ok()
.expect("Failed to read line");
let input_num: Option<uint> = input.parse();
let num = match input_num {
Some(num) => num,
None => {
println!("Please input a number!");
return;
}
};
println!("You guessed: {}", num);
match cmp(num, secret_number) {
Less => println!("Too small!"),
Greater => println!("Too big!"),
Equal => println!("You win!"),
}
}
fn cmp(a: uint, b: uint) -> Ordering {
if a < b { Less }
else if a > b { Greater }
else { Equal }
}
Compiling, it raises the following:
/home/daniel/Projects/guessing_game/src/main.rs:16:41: 16:48 error: type
collections::string::String
does not implement any method in scope namedparse
/home/daniel/Projects/guessing_game/src/main.rs:16 let input_num: Option = input.parse();
rustc 0.13.0-dev (29ad8539b 2014-12-24 16:21:23 +0000)
. You may want to update your rustc to match the docs. – Shepmaster