expression

Rust Access and update struct fields

Use a dot and the field name to read a named part of a struct. A mutable struct can allow direct updates of its fields. Expected output (not executed): 7 Why: The field is updated in place. Common mistake: A field is not a method; `item.count()` would try method lookup instead.

Formal Syntax & Structure

rust syntaxrust
value.field

Working Example

Example Snippetrust
struct Stock { count: i32 }
fn main() {    let mut item = Stock { count: 4 };    item.count += 3;    println!("{}", item.count);}
Rust Reference