You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
When I read the number by read_byte, it prints I guess ASCII number.. #3479
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.
fnmain(){//First, we read the input linelet my_number_string = (io::stdin()as io::ReaderUtil).read_line();
io::println(my_number_string);//Then, we try to turn it into an intlet maybe_my_int = int::from_str(my_number_string);if(option::is_some(maybe_my_int)){match option::unwrap(maybe_my_int){0 => io::println("zero"),1|2 => io::println("one or two"),3..10 => io::println("three to ten"),
_ => io::println("something else")}}else{
io::println("Not a number!");}}
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