|
1 | 1 | // Silence some warnings so they don't distract from the exercise.
|
2 |
| -#![allow(dead_code, unused_mut, unused_variables)] |
| 2 | +#![allow(unused_mut, unused_variables)] |
3 | 3 |
|
4 | 4 | fn main() {
|
5 |
| - // This collects any command-line arguments into a vector of Strings. |
6 |
| - // For example: |
| 5 | + // 1. Use an unconditional `loop` to count how many times we can double `bunnies` until there |
| 6 | + // are over 500 bunnies. (Hint: The answer is 8 times) |
7 | 7 | //
|
8 |
| - // cargo run apple banana |
9 |
| - // |
10 |
| - // ...produces an 'args' variable with the equivalent of: |
11 |
| - // |
12 |
| - // vec!["apple".to_string(), "banana".to_string()] |
13 |
| - let args: Vec<String> = std::env::args().skip(1).collect(); |
14 |
| - |
15 |
| - // This consumes the `args` vector to iterate through each String |
16 |
| - for arg in args { |
17 |
| - // 1a. Your task: handle the command-line arguments! |
18 |
| - // |
19 |
| - // - If arg is "sum", then call the sum() function |
20 |
| - // - If arg is "double", then call the double() function |
21 |
| - // - If arg is anything else, then call the count() function, passing "arg" to it. |
22 |
| - |
23 |
| - // 1b. Now try passing "sum", "double" and "bananas" to the program by adding your argument |
24 |
| - // after "cargo run". For example "cargo run sum" |
25 |
| - } |
26 |
| -} |
27 |
| - |
28 |
| -fn sum() { |
29 |
| - let mut sum = 0; |
30 |
| - // 2. Use a "for loop" to iterate through integers from 7 to 23 *inclusive* using a range |
31 |
| - // and add them all together (increment the `sum` variable). Hint: You should get 255 |
32 |
| - // Run it with `cargo run sum` |
33 |
| - |
34 |
| - println!("The sum is {}", sum); |
35 |
| -} |
| 8 | + // Inside the loop: |
| 9 | + // - Add 1 to `count` |
| 10 | + // - Multiply `bunnies` by 2 |
| 11 | + // - If `bunnies` is larger than 500, break out of the loop. |
36 | 12 |
|
37 |
| -fn double() { |
38 | 13 | let mut count = 0;
|
39 |
| - let mut x = 1; |
40 |
| - // 3. Use a "while loop" to count how many times you can double the value of `x` (multiply `x` |
41 |
| - // by 2) until `x` is larger than 500. Increment `count` each time through the loop. Run it |
42 |
| - // with `cargo run double` Hint: The answer is 9 times. |
| 14 | + let mut bunnies = 2; |
| 15 | + |
| 16 | + // (write your `loop` here) |
43 | 17 |
|
44 | 18 | println!(
|
45 |
| - "You can double x {} times until x is larger than 500", |
| 19 | + "Bunnies doubled {} times before there were more than 500", |
46 | 20 | count
|
47 | 21 | );
|
48 |
| -} |
49 | 22 |
|
50 |
| -fn count(arg: String) { |
51 |
| - // Challenge: Use an unconditional loop (`loop`) to print `arg` 8 times, and then break. |
52 |
| - // You will need to count your loops, somehow. Run it with `cargo run bananas` |
| 23 | + // 2. Use a `for` loop to iterate through integers from 7 to 23 *inclusive* using a range |
| 24 | + // and add them all together (add each value to the `sum` variable). Hint: You should get 255 |
| 25 | + |
| 26 | + let mut sum = 0; |
| 27 | + |
| 28 | + // (write the `for` loop here) |
| 29 | + |
| 30 | + println!("The sum is {}", sum); |
| 31 | + |
| 32 | + // 3. Use a `while` loop to create a vector containing the first 12 multiples of 5. |
| 33 | + // |
| 34 | + // The loop should continue until `fives.len()` returns 12. |
| 35 | + // |
| 36 | + // Each time through the loop, call `fives.push( ... )` to push `current_five` onto the vector, |
| 37 | + // and then add 5 to `current_five`. |
| 38 | + |
| 39 | + let mut fives: Vec<i32> = vec![]; |
| 40 | + let mut current_five = 5; |
| 41 | + |
| 42 | + // (write the `while` loop here) |
| 43 | + |
| 44 | + println!("Here are the first 12 multiples of 5: {:?}", fives); |
| 45 | + |
| 46 | + // 4. Use `if`, `else if` and `else` inside the `for` loop below to do the following: |
53 | 47 | //
|
54 |
| - // print!("{} ", arg); // Execute this line 8 times, and then break. `print!` doesn't add a newline. |
| 48 | + // - If the number is 0, then add 7 to `total` |
| 49 | + // - If the number is 1 or 2 then add 30 to `total` |
| 50 | + // - If the number is anything else, subtract 5 from `total` |
| 51 | + // |
| 52 | + // Hint: The total should be 52 |
55 | 53 |
|
56 |
| - println!(); // This will output just a newline at the end for cleanliness. |
| 54 | + let mut total = 0; |
| 55 | + let numbers = vec![0, 1, 2, 3, 4, 5]; |
| 56 | + for number in numbers { |
| 57 | + // (write your `if/else` expression here) |
| 58 | + } |
| 59 | + |
| 60 | + println!("The total is {}", total); |
| 61 | + |
| 62 | + // Challenge: Change the implementation of your answers to #1-#3 as follows: |
| 63 | + // |
| 64 | + // - Change #1 to use `while` |
| 65 | + // - Change #2 to use `loop` |
| 66 | + // - Change #3 to use `for` and a range (multiply the range value by 5 inside your loop before |
57 | 67 | }
|
0 commit comments