Rust is a programming language that combines speed and control with memory safety. Its compiler checks code before it runs. An error can feel frustrating, but the compiler often points to the problem and suggests a fix. section 1.1
Start with a small program, then change one thing at a time. If it stops compiling, read the message: it may identify a mismatched type, missing punctuation, or a variable that needs to be mutable. section 1.1.2
error: expected `,`, found `my_name` | 4 | println!("{}" my_name); | ^^^^^^^ expected `,` error[E0308]: mismatched types --> src/main.rs:2:27 | 2 | let my_name: String = "Dave"; | ------ ^^^^^^- help: try using a conversion method: `.to_string()` | | | | | expected struct `String`, found `&str` | expected due to this error[E0308]: mismatched types --> src/main.rs:3:18 | 3 | my_name.push("!"); | ---- ^^^ expected `char`, found `&str` | | | arguments to this function are incorrect | help: if you meant to write a `char` literal, use single quotes | 3 | my_name.push('!'); | ~~~
These errors suggest a string conversion, a character literal, and a comma in println!. Fixing them reveals one more issue: changing the string requires mut. section 1.1.2
fn main() {
let mut my_name: String = "Dave".to_string();
my_name.push('!');
println!("{}", my_name);
}
The corrected program declares the string mutable, adds a character, and prints the result. section 1.1.2
A few basics make those messages easier to understand:
- let gives data a name. Rust can often infer its type, while mut permits changing the variable. section 1.12
- Integer types distinguish signed from unsigned values and specify how many bits they use. String length counts bytes, which need not equal the number of characters. section 1.3
- println! prints values; {} uses Display formatting, while {:?} uses Debug formatting when available. Comments help people read code and can also generate documentation. section 1.8 section 1.2
Rust can be used for small tools as well as larger software. The practical habit is to write a little code, let the compiler check it, and work through its messages one at a time. section 24.4 section 1.1.1





