liveBook · Manning

Rust

As explained in Learn Rust in a Month of Lunches

Rust is a compiled systems programming language emphasizing memory safety, performance, and explicit control over low-level data representation.

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

How Learn Rust in a Month of Lunches covers it

How deep it goes
Works through types, printing, variables, and compiler errors, while deferring fuller treatment of traits and printing.
Examples
Shows a string program corrected through compiler messages and strings whose byte lengths differ from their character counts.
What it assumes
Starts with basic syntax and defines variables and primitive types, explaining binary rather than assuming it.
What you can do after
Practice/perform it: write small Rust programs and use compiler suggestions to correct type, formatting, and mutability errors.
How it connects
Connects Rust to primitive types, type inference, mutability, scopes, traits, and error handling.

Also explained in

Compare how 6 books explain Rust →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Rust in a Month of Lunches. The section links point to the parts of the book it draws on.