Closed

Description
How can I read pure decimal?
Please Let me know it .
I attach source below.
fn main(){
let my_number = io::stdin().read_byte() as int;
// let mut my_number = 0;
io::println(int::str(my_number));
match my_number{
0 => io::println("zero"),
1|2 => io::println("one or two"),
3..10 => io::println("three to ten"),
_ => io::println("something else")
}
}
OUTPUT
1
49
something else
Activity
AngryLawyer commentedon Sep 13, 2012
Read byte only gets you a single byte (i.e the first ascii character you type in), so gives you a number. You need read_line, which is in ReaderUtil, which Reader can be cast to (I think this is the right way to do it).
After that, you can try to convert the string into an int. Someone might put something other than an int in, though, and to stop everything blowing up, Rust instead returns an option instead - which means it might be there, or might not be. So, you check it with 'is_some', to make sure it's really there, and then can unwrap it to get the juicy, juicy number.
Merge pull request rust-lang#3479 from sinkuu/issue_2995
Auto merge of rust-lang#3479 - rust-lang:rustup-2024-04-17, r=RalfJung