Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Operators and Expressions

The . member access operator

Reference for the Rust dot operator: field access, tuple fields, method calls, automatic referencing and dereferencing, method resolution order and ambiguity.

The dot operator reaches into a value: p.x reads a field, pair.0 reads a tuple field and text.len() calls a method. Unlike C, Rust has no separate -> operator. The compiler adjusts the left side for you, automatically taking references and following Deref chains until it finds a field or method with that name.

This auto-ref and auto-deref is why boxed.len(), rc_string.len() and reference_to_reference.len() all work with the same syntax. It applies only to the left side of .; function arguments get the weaker deref coercion instead, and operators like + get none.

Rust
let name = user.name;          // field
let first = pair.0;            // tuple field
let n = text.trim().len();     // chained method calls
let ready = future.await;      // also a dot form

Try it

Rust Fields, methods and auto-deref

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Ambiguity and explicit calls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Syntax

Rust
value.field          // named field
value.0              // tuple or tuple-struct field
value.method(args)   // method call
value.method::<T>()  // method call with explicit generics
value.await          // await a future

Method lookup: for the receiver expression, the compiler builds a list of candidate types by repeatedly dereferencing (T, *T, **T, ...), and finally applies an unsizing step (array to slice). For each candidate it tries the method with a receiver of type T, then &T, then &mut T. The first match wins, and inherent methods are tried before trait methods at the same step.

Errors: E0609 (no such field), E0599 (no such method), E0616 (private field), E0615 (a method named like a field, used without parentheses), E0034 (ambiguous between traits), E0382/E0507 (the method takes self and the value was borrowed or already moved).

Usage notes

  • Fields are not auto-borrowed. p.x reads or moves the field x; &p.x borrows it. A non-Copy field cannot be moved out of a reference.
  • Disjoint field borrows: &mut p.a and &p.b can coexist, but two method calls that each take &mut self cannot, because the borrow covers the whole struct.
  • Receiver kinds decide the move: self consumes the value, &self and &mut self borrow it. Chained builder methods (b.name("x").size(3)) rely on self or &mut self returns.
  • Trait methods need the trait in scope. use std::io::Write; is required before .write_all() works, even though the type implements it.
  • Name clashes: inherent beats trait, trait beats nothing, and two traits with the same method name make a bare call ambiguous. Use Trait::method(&x) or <Type as Trait>::method(&x).
  • Numeric literals: calling an inherent method on an unsuffixed literal (2.pow(3), 2.0.sqrt()) fails with E0689 because the type is not known yet. Add a suffix (2_i32.pow(3), 2.0_f64.sqrt()). Trait methods such as 1.max(2) fall back to i32.
  • .await is postfix so that chains such as client.get(url).send().await?.text().await? read left to right.
  • Cost: auto-ref and auto-deref are resolved at compile time. Deref impls run as normal (usually inlined) function calls.