Skip to content

Commit ea96a93

Browse files
authored
Merge pull request #115 from ferrous-systems/fizbuzz-format-not-string-from
Clean up fizzbuzz.
2 parents 486cfe5 + 931a4d7 commit ea96a93

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

exercise-book/src/fizzbuzz.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ If you need it, we have provided a [complete solution](../../exercise-solutions/
4141
The recommended way to print to the console in this exercise is `println!`. `println!` *always* needs a format string - it uses `{}` as a placeholder to mean **print the next argument**, like Python 3 or C#.
4242

4343
```rust
44-
let s = String::from("Fizz");
44+
let s = "Fizz";
4545
println!("The value is s is {}. That's nice.", s);
4646
```
4747

@@ -51,18 +51,20 @@ The two recommended ways to get a `String` type for this exercise are:
5151

5252
```rust
5353
// 1.
54-
let string = String::from("Fizz");
54+
let s = "Fizz".to_string();
5555

5656
let i = 4;
57-
let string = i.to_string();
57+
let s = i.to_string();
5858

5959
// 2.
60-
let string = format!("Buzz");
60+
let s = format!("Fizz");
6161

6262
let i = 4;
63-
let string = format!("{}", i);
63+
let s = format!("{}", i);
6464
```
6565

66+
We'll cover these in more detail later, but either can be used to convert string literals (`"hello"`) and integers (`123`) into values of type `String`, which is all you'll need here. We'll use `format!` in our examples. It's flexible and equally efficient as other methods.
67+
6668
### Returning data
6769

6870
If you have issues returning data from multiple branches of your solution, liberally use `return`.
@@ -71,11 +73,10 @@ If you have issues returning data from multiple branches of your solution, liber
7173
# fn returner() -> String {
7274
# let x = 10;
7375
if x % 5 == 0 {
74-
return String::from("Buzz");
76+
return format!("Buzz");
7577
}
76-
String::from("Fizz")
78+
format!("Fizz")
7779
# }
78-
7980
```
8081

8182
## Step-by-Step-Solution
@@ -164,7 +165,7 @@ fn fizzbuzz(i: u32) -> String {
164165

165166
### Step 4: Call the function
166167

167-
Add the function call to `fn fizzbuzz()` to the formatted string in the `println!()` statement.
168+
Add the function call to `fn fizzbuzz()` to the formatted string in the `println!()` statement.
168169

169170
Running this code should print numbers, interlaced with `Fizz`, `Buzz` and `FizzBuzz` according to the rules mentioned above.
170171

0 commit comments

Comments
 (0)