Start with the idea
A method call uses dot syntax, and Rust automatically borrows or dereferences the receiver when the signature makes that unambiguous. Chaining is readable when each step produces the value the next step expects. Watch ownership: some methods borrow the receiver, while others consume it and return a new value.
Read the code
let name = String::from(" crab ");
let clean = name.trim().to_uppercase();
println!("{clean}");trim borrows; to_uppercase creates a new owned String.
Practice starter
let text = String::from("rust");
let size = text.len();
println!("{text} {size}");
Work through it
Start at the receiver, the value to the left of the dot. In name.trim(), trim borrows the string and returns a slice (&str) of its contents; it does not edit name. The next call, to_uppercase(), reads that slice and creates a new String. You can still print the original value after both calls. A chain is easiest to reason about when you can name the type after each step: String → &str → String.
Method syntax is a convenient way to pass the receiver as the first argument. text.len() and str::len(text) express closely related calls; the dot form also lets Rust apply an unambiguous borrow or dereference. It does not erase ownership rules. A method declared with self takes the receiver by value; one with &self only reads it; one with &mut self may modify it. When a value seems to disappear after a method call, inspect that signature before inserting a clone.
For practice, write the chain as separate bindings: let trimmed = name.trim(); let upper = trimmed.to_uppercase();. Print all three values and predict which ones own their bytes. Then change a mutable string with push_str and compare that in-place edit with making a new uppercase string. Prefer a short chain when each conversion is obvious; use named intermediate values when you need to inspect a failure, annotate a type, or explain the transformation to a teammate.
A method call is still a function call with an explicit receiver. value.len() reads as an operation associated with value; in an impl block, the receiver is commonly written &self, &mut self, or self. These forms mean borrow for reading, borrow for mutation, or take ownership. The receiver choice determines what the caller may do with the value afterward, so read it as part of the method's contract.
Call one read-only method twice on the same value and predict why both calls work. Then write or inspect a method that takes self and try to reuse the old binding afterward. A compiler error here is about ownership, not about the dot syntax. Methods can return values, mutate state, or do I/O; the dot alone does not tell you which. Look at the signature and name together. Prefer short methods that preserve a clear invariant over a large chain whose intermediate effects are hard to see.
Try it
Chain two text methods and identify which step allocates a new String.
What success looks like: You can tell whether each receiver is borrowed or consumed.
Check your understanding
Why is text still usable after len()?
Reveal the answer
- len borrows self rather than consuming it
- usize restores the String
- All methods copy their receiver
The receiver is &self, so the method reads through a temporary borrow.
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Using Methods (courseId "basics", lessonId "methods-intro") Link: https://crabrix.com/learn/basics/methods-intro/ Goal: Read a method's self parameter to learn its ownership effect. Practice: open the starter in the Crabrix Playground (https://crabrix.com/playground/?lesson=basics/methods-intro) and I will write the code there. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.