Skip to content

Commit 98524c7

Browse files
committed
Replace to_str() with to_string()
- Renamed as of rust-lang/rust#15075
1 parent a936fde commit 98524c7

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

book/chapter-05.md

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -442,27 +442,27 @@ fn main() {
442442
for num in range(1i, 101) {
443443
let answer =
444444
if div_by_fifteen(num){
445-
"FizzBuzz".to_str()
445+
"FizzBuzz".to_string()
446446
}
447447
else if div_by_three(num) {
448-
"Fizz".to_str()
448+
"Fizz".to_string()
449449
}
450450
else if div_by_five(num) {
451-
"Buzz".to_str()
451+
"Buzz".to_string()
452452
}
453453
else {
454-
num.to_str()
454+
num.to_string()
455455
};
456456
457457
println!("{}", answer);
458458
}
459459
}
460460
~~~
461461

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`,
463463
which is a heap allocated string with dynamic length, and `&str`, which
464464
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`.
466466

467467
Before, we could get away with a `&str`, because they all had the same
468468
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:
474474
fn main() {
475475
for num in range(1i, 101) {
476476
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() }
481481
);
482482
}
483483
}

0 commit comments

Comments
 (0)