liveBook · Manning

Let keyword

As explained in Rust in Action: Systems programming concepts and techniques

A let binding introduces a local Rust variable binding whose associated value may have internally mutable state even when the binding itself is immutable.

Use `let` to declare a local variable binding. A binding is immutable by default: `let a = 10;` gives `a` a value without making the binding read-write. The semicolon ends the statement. section 2.2.1

Listing 2.2 Adding integers using variables and declaring types
 fn main() {                             #1
   let a = 10;                           #2
   let b: i32 = 20;                      #3
   let c = 30i32;                        #4
   let d = 30_i32;                       #5
   let e = add(add(a, b), add(c, d));
 
   println!("( a + b ) + ( c + d ) = {}", e);
 }
 
 fn add(i: i32, j: i32) -> i32 {         #6
   i + j                                 #7
 }

The integer-addition program shows inferred types (`let a = 10;`), an explicit type (`let b: i32 = 20;`), and types attached to numeric literals. section 2.2.1

An immutable binding does not mean every part of its value must remain unchanged. Some types, including `Arc` and `Rc`, can update an internal reference count while presenting an immutable interface. This is called interior mutability and helps explain why `let` and `const` are distinct. section 3.4.1

Borrowing adds another distinction: multiple read-only references may point to the same data, while mutable borrows are guaranteed not to alias it. section 3.4.1

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains declaration syntax, type annotations, default immutability, and its distinction from interior mutability and borrowing; it does not develop those mechanisms further.
Examples
Uses an integer-addition program with several declaration forms and `Arc` and `Rc` reference counts as a contrasting case.
What it assumes
Introduces variable declarations and function calls from their syntax, without assuming prior Rust knowledge.
What you can do after
Evaluate/understand whether a Rust declaration needs an explicit type and why an immutable binding can hold internally changing state.
How it connects
Connects variable bindings to type inference, type annotations, lexical scopes, interior mutability, and borrowing.

Also explained in

Compare how 2 books explain let keyword →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.