Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No method parse for String

Tags:

rust

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 named parse /home/daniel/Projects/guessing_game/src/main.rs:16 let input_num: Option = input.parse();

like image 627
vise Avatar asked Apr 21 '26 15:04

vise


1 Answers

This is just a question of rustc versions: the parse method was added ~2 days ago, but your compiler is about 3 days old. Rust moves fast and it's easy to hit little cases like this when working at the bleeding edge unfortunately.

Hopefully this will really start to settle from the 9th of January.

like image 85
huon Avatar answered Apr 25 '26 09:04

huon