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.
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
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.
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
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.xreads or moves the fieldx;&p.xborrows it. A non-Copyfield cannot be moved out of a reference. - Disjoint field borrows:
&mut p.aand&p.bcan coexist, but two method calls that each take&mut selfcannot, because the borrow covers the whole struct. - Receiver kinds decide the move:
selfconsumes the value,&selfand&mut selfborrow it. Chained builder methods (b.name("x").size(3)) rely onselfor&mut selfreturns. - 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 as1.max(2)fall back toi32. .awaitis postfix so that chains such asclient.get(url).send().await?.text().await?read left to right.- Cost: auto-ref and auto-deref are resolved at compile time.
Derefimpls run as normal (usually inlined) function calls.
Related
- * dereference
- & and &mut
- ::<> turbofish
- impl
- Tutorial: Methods, Structs, Traits