Skip to content
tezvyn:
Menu

Rust Methods: Attaching Behavior to Data

Source: doc.rust-lang.orgMediumHow cards are made

Rust methods are functions attached to your data structures, defined in an impl block. Instead of do_thing(my_struct), you call my_struct.do_thing(). The key footgun: instance.name() calls a method, but instance.name accesses a field.

Why it exists

To solve code organization. Instead of scattering functions that operate on a data type throughout a codebase, methods group all related behaviors directly with the data type itself inside an impl block. This makes the capabilities of a type easy to discover and manage.

The mental model

Think of methods as verbs for your nouns (structs). If a Rectangle struct is your data, its area() method is an action it can perform. Methods are defined in an impl (implementation) block, formally tying the behavior to the data structure it operates on.

How it works

Methods are functions defined inside an impl block whose first parameter is self, representing the instance of the type. This parameter specifies ownership: &self borrows immutably for reading, &mut self borrows mutably for modification, and self takes ownership, consuming the instance. When you call my_instance.my_method(), Rust automatically passes the instance as the first argument, a convenience known as method syntax. The type Self (with a capital S) inside an impl block is an alias for the type the block is for, so &self is shorthand for self: &Self.

When to use it

This is the idiomatic way to define behavior on structs and enums in Rust. Use methods to encapsulate logic that operates on an instance of a type. This leads to cleaner, more organized code compared to using standalone functions; for example, rect.area() is preferred over area(rect).

When not to use it

While methods are the default, a standalone function is better for logic that isn't tied to a single instance's state. If a function is a generic utility or operates on multiple unrelated types, it doesn't belong in an impl block for one specific type. Functions within an impl block that do not take self as a parameter are called associated functions, often used as constructors like String::new().

One canonical example

Given a struct Rectangle { width: u32, height: u32 }, you can define a method in an impl Rectangle block: fn area(&self) -> u32 { self.width * self.height }. This is called on an instance like let my_rect = Rectangle { ... }; let a = my_rect.area();. A common footgun is name collision. You could also have a method fn width(&self) -> bool { self.width > 0 }. Rust distinguishes by syntax: my_rect.width() calls the method, while my_rect.width accesses the field.

Interview question

In Rust, what is the key distinction between `my_instance.name()` and `my_instance.name`?

  • a.my_instance.name() is a syntax error, as methods cannot have the same name as fields.
  • b.my_instance.name() is used for mutable access, while my_instance.name is for immutable access.
  • c.my_instance.name() calls a method, while my_instance.name accesses a field.Correct
  • d.my_instance.name() accesses a field, while my_instance.name calls an associated function.
Why?

The card explicitly states, "The key footgun: instance.name() calls a method, but instance.name accesses a field." This highlights how Rust's syntax differentiates between invoking a method and accessing a struct field, even if they share the same identifier. Option A is incorrect because the card shows methods and fields can share names.

Just read this? Test yourself on what you have been reading.

Read the original → doc.rust-lang.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on rust — each one lists the topics its interview covers.

See open roles