Small Programs · 10 min · free lesson

Using Methods

Method syntax puts an operation beside the value it acts on.

Core idea

Read a method's self parameter to learn its ownership effect.

Coming from C? Structs and Methods covers this side by side. · Coming from Node.js? Classes and objects vs structs covers this side by side.

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}");

Practise this code in Playground ↗

Diagram for Using Methods: a Rust code example flows to the lesson rule and the key observation.
trim borrows; to_uppercase creates a new owned String.

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
  1. len borrows self rather than consuming it
  2. usize restores the String
  3. All methods copy their receiver

The receiver is &self, so the method reads through a temporary borrow.

One more experiment

Remove .trim() and predict the exact stdout, spaces included. Then add let bytes = name.into_bytes(); before printing name and write the diagnostic you expect, since into_bytes takes self.

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.

In Claude Code, Codex or Cursor, paste it into a chat with Crabrix Academy connected. On a phone, the Claude and ChatGPT apps open the same links.