Activity 5: Compiler Error Hunt
What you are doing
Working in pairs, in the Rust Playground: https://play.rust-lang.org
Break the program below as many different ways as you can, and read what the compiler says each time.
Goal: at least 8 different compiler errors. There is room for 12 on your sheet if you get on a roll.
Why we use the Playground
VS Code underlines your mistake before you ever compile. That is useful most of the time, but not today, because today's exercise is all about hitting the errors and reading them!
It's also a lot faster to get going.
The program
Paste this in and Run it first, so you know it works before you touch it.
fn main() { let temps_c = vec![14.0, 17.5, 21.0, 19.5, 23.0]; let mut warm_days = 0; for i in 0..temps_c.len() { let c = temps_c[i]; let f = c * 9.0 / 5.0 + 32.0; println!("Day {}: {:.1}C is {:.1}F", i + 1, c, f); if c > 18.0 { warm_days += 1; } } let total: f64 = temps_c.iter().sum(); let mean = total / temps_c.len() as f64; println!("Mean high: {:.1}C", mean); println!("Warm days: {} of {}", warm_days, temps_c.len()); }
It should print five days, a mean of 19.0C, and 3 warm days.
The activity loop
- Make one change that breaks the code
- Run it
- Read the error over
- Write it on your half sheet: what you changed, and what the error is (in brief)
- Undo your change, and break it a different way
Make just one change at a time so you can tell what error message belongs to what.
Some ideas
- Change something's type
- Take a keyword away
- Mess with punctuation and variable names
- Ask for something that does not exist
- Give
println!the wrong number of things to print
Warnings are not errors. If you run Clippy from the Tools menu it will suggest ways to rewrite code that already works. Interesting, but it does not count for the hunt.
The compiler errors we found
- Drop
mutfromwarm_days: E0384, cannot assign twice to immutable variable - Type
totalasi32: E0277, a value of typei32cannot be made by summing over&{float} - Drop
as f64: E0277, cannot dividef64byusize - Delete a semicolon: expected
;, foundprintln - Misspell
temps_cin the loop: E0425, cannot find value in this scope - Write
if c > 18: E0277, can't compare{float}with{integer} - Remove one argument from
println!: 3 positional arguments in format string, but there are 2 - Write
warm_days += 1.0: E0277, cannot add-assign{float}to{integer} - Put
"17.5"in the vec: E0308, mismatched types - Rename
main: E0601,mainfunction not found - Delete a closing brace: this file contains an unclosed delimiter
- Index
temps_c[i + 1]: compiles, then panics at runtime with index out of bounds - Delete a
!: E0423: expected function, found macro - Try to index like
0[0]: E0608 cannot index into a value of type{integer} - Add
.len()tomean: E0599, no method namedlenfound for typef64in the current scope
Warnings and non-errors too:
- Add
mut: warns you don't need it - Delete
lnfromprintln!: it still works (but prints funny) - Delete
Vec!: nothing seems to change (but the type oftemps_cchanges)