@@ -2257,10 +2257,10 @@ a function for that:
22572257let input = io::stdin().read_line ()
22582258 .ok ()
22592259 .expect(" Failed to read line" );
2260- let input_num: Option< uint> = from_str( input.as_slice () );
2260+ let input_num: Option< uint> = input.parse ( );
22612261` ` `
22622262
2263- The ` from_str ` function takes in a ` & str` value and converts it into something.
2263+ The ` parse ` function takes in a ` & str` value and converts it into something.
22642264We tell it what kind of something with a type hint. Remember our type hint with
22652265` random()` ? It looked like this:
22662266
@@ -2279,8 +2279,8 @@ In this case, we say `x` is a `uint` explicitly, so Rust is able to properly
22792279tell ` random()` what to generate. In a similar fashion, both of these work:
22802280
22812281` ` ` {rust,ignore}
2282- let input_num = from_str ::< uint>( " 5 " ) ; // input_num: Option< uint>
2283- let input_num: Option< uint> = from_str( " 5" ) ; // input_num: Option< uint>
2282+ let input_num = " 5 " .parse ::< uint>( ) ; // input_num: Option< uint>
2283+ let input_num: Option< uint> = " 5" .parse() ; // input_num: Option< uint>
22842284` ` `
22852285
22862286Anyway, with us now converting our input to a number, our code looks like this:
@@ -2301,7 +2301,7 @@ fn main() {
23012301 let input = io::stdin().read_line ()
23022302 .ok ()
23032303 .expect(" Failed to read line" );
2304- let input_num: Option< uint> = from_str( input.as_slice () );
2304+ let input_num: Option< uint> = input.parse ( );
23052305
23062306 println! (" You guessed: {}" , input_num);
23072307
@@ -2350,7 +2350,7 @@ fn main() {
23502350 let input = io::stdin().read_line()
23512351 .ok()
23522352 .expect("Failed to read line");
2353- let input_num: Option<uint> = from_str( input.as_slice() );
2353+ let input_num: Option<uint> = input.parse( );
23542354
23552355 let num = match input_num {
23562356 Some(num) => num,
@@ -2395,7 +2395,7 @@ Uh, what? But we did!
23952395
23962396... actually, we didn' t. See, when you get a line of input from `stdin()`,
23972397you get all the input. Including the `\n` character from you pressing Enter.
2398- Therefore, `from_str ()` sees the string `"5\n"` and says "nope, that' s not a
2398+ Therefore, `parse ()` sees the string `"5\n"` and says "nope, that' s not a
23992399number; there' s non-number stuff in there!" Luckily for us, `&str`s have an easy
24002400method we can use defined on them: `trim()`. One small modification, and our
24012401code looks like this:
@@ -2416,7 +2416,7 @@ fn main() {
24162416 let input = io::stdin().read_line()
24172417 .ok()
24182418 .expect("Failed to read line");
2419- let input_num: Option<uint> = from_str( input.as_slice ().trim() );
2419+ let input_num: Option<uint> = input.trim ().parse( );
24202420
24212421 let num = match input_num {
24222422 Some(num) => num,
@@ -2491,7 +2491,7 @@ fn main() {
24912491 let input = io::stdin().read_line ()
24922492 .ok ()
24932493 .expect(" Failed to read line" );
2494- let input_num: Option< uint> = from_str( input.as_slice ().trim () );
2494+ let input_num: Option< uint> = input.trim ().parse ( );
24952495
24962496 let num = match input_num {
24972497 Some(num) => num,
@@ -2566,7 +2566,7 @@ fn main() {
25662566 let input = io::stdin().read_line()
25672567 .ok()
25682568 .expect("Failed to read line");
2569- let input_num: Option<uint> = from_str( input.as_slice ().trim() );
2569+ let input_num: Option<uint> = input.trim ().parse( );
25702570
25712571 let num = match input_num {
25722572 Some(num) => num,
@@ -2621,7 +2621,7 @@ fn main() {
26212621 let input = io::stdin().read_line()
26222622 .ok()
26232623 .expect("Failed to read line");
2624- let input_num: Option<uint> = from_str( input.as_slice ().trim() );
2624+ let input_num: Option<uint> = input.trim ().parse( );
26252625
26262626 let num = match input_num {
26272627 Some(num) => num,
@@ -2697,7 +2697,7 @@ fn main() {
26972697 let input = io::stdin().read_line()
26982698 .ok()
26992699 .expect("Failed to read line");
2700- let input_num: Option<uint> = from_str( input.as_slice ().trim() );
2700+ let input_num: Option<uint> = input.trim ().parse( );
27012701
27022702 let num = match input_num {
27032703 Some(num) => num,
0 commit comments