@@ -442,27 +442,27 @@ fn main() {
442
442
for num in range(1i, 101) {
443
443
let answer =
444
444
if div_by_fifteen(num){
445
- "FizzBuzz".to_str ()
445
+ "FizzBuzz".to_string ()
446
446
}
447
447
else if div_by_three(num) {
448
- "Fizz".to_str ()
448
+ "Fizz".to_string ()
449
449
}
450
450
else if div_by_five(num) {
451
- "Buzz".to_str ()
451
+ "Buzz".to_string ()
452
452
}
453
453
else {
454
- num.to_str ()
454
+ num.to_string ()
455
455
};
456
456
457
457
println!("{}", answer);
458
458
}
459
459
}
460
460
~~~
461
461
462
- Why the "` to_str ()` "s? There are two types of Strings in Rust: ` Str ` ,
462
+ Why the "` to_string ()` "s? There are two types of Strings in Rust: ` Str ` ,
463
463
which is a heap allocated string with dynamic length, and ` &str ` , which
464
464
is a borrowed, immutable view into a string. The literal is of type ` &str ` ,
465
- but we want a ` Str ` . ` to_str ()` turns a ` &str ` into a ` String ` .
465
+ but we want a ` Str ` . ` to_string ()` turns a ` &str ` into a ` String ` .
466
466
467
467
Before, we could get away with a ` &str ` , because they all had the same
468
468
type. But since we've added an arm with an ` int ` , we need to make them all
@@ -474,10 +474,10 @@ Because the `if` returns a value, we could also do something like this:
474
474
fn main() {
475
475
for num in range(1i, 101) {
476
476
println!("{:s}",
477
- if div_by_fifteen(num) { "FizzBuzz".to_str () }
478
- else if div_by_three(num) { "Fizz".to_str () }
479
- else if div_by_five(num) { "Buzz".to_str () }
480
- else { num.to_str () }
477
+ if div_by_fifteen(num) { "FizzBuzz".to_string () }
478
+ else if div_by_three(num) { "Fizz".to_string () }
479
+ else if div_by_five(num) { "Buzz".to_string () }
480
+ else { num.to_string () }
481
481
);
482
482
}
483
483
}
0 commit comments