Method Syntax - The Rust Programming Language (original) (raw)

  1. Foreword
  2. Introduction
  3. 1. Getting Started
    1. 1.1. Installation
    2. 1.2. Hello, World!
    3. 1.3. Hello, Cargo!
  4. 2. Programming a Guessing Game
  5. 3. Common Programming Concepts
    1. 3.1. Variables and Mutability
    2. 3.2. Data Types
    3. 3.3. How Functions Work
    4. 3.4. Comments
    5. 3.5. Control Flow
  6. 4. Understanding Ownership
    1. 4.1. What is Ownership?
    2. 4.2. References & Borrowing
    3. 4.3. Slices
  7. 5. Using Structs to Structure Related Data
    1. 5.1. Defining and Instantiating Structs
  8. 5.2. An Example Program Using Structs
  9. 5.3. Method Syntax
  10. 6. Enums and Pattern Matching
    1. 6.1. Defining an Enum
  11. 6.2. The match Control Flow Operator
  12. 6.3. Concise Control Flow with if let
  13. 7. Packages, Crates, and Modules
    1. 7.1. Packages and crates for making libraries and executables
  14. 7.2. Modules and use to control scope and privacy
  15. 8. Common Collections
    1. 8.1. Vectors
  16. 8.2. Strings
  17. 8.3. Hash Maps
  18. 9. Error Handling
    1. 9.1. Unrecoverable Errors with panic!
  19. 9.2. Recoverable Errors with Result
  20. 9.3. To panic! or Not To panic!
  21. 10. Generic Types, Traits, and Lifetimes
    1. 10.1. Generic Data Types
  22. 10.2. Traits: Defining Shared Behavior
  23. 10.3. Validating References with Lifetimes
  24. 11. Testing
    1. 11.1. Writing tests
  25. 11.2. Running tests
  26. 11.3. Test Organization
  27. 12. An I/O Project: Building a Command Line Program
    1. 12.1. Accepting Command Line Arguments
  28. 12.2. Reading a File
  29. 12.3. Refactoring to Improve Modularity and Error Handling
  30. 12.4. Developing the Library’s Functionality with Test Driven Development
  31. 12.5. Working with Environment Variables
  32. 12.6. Writing Error Messages to Standard Error Instead of Standard Output
  33. 13. Functional Language Features: Iterators and Closures
    1. 13.1. Closures: Anonymous Functions that Can Capture Their Environment
  34. 13.2. Processing a Series of Items with Iterators
  35. 13.3. Improving Our I/O Project
  36. 13.4. Comparing Performance: Loops vs. Iterators
  37. 14. More about Cargo and Crates.io
    1. 14.1. Customizing Builds with Release Profiles
  38. 14.2. Publishing a Crate to Crates.io
  39. 14.3. Cargo Workspaces
  40. 14.4. Installing Binaries from Crates.io with cargo install
  41. 14.5. Extending Cargo with Custom Commands
  42. 15. Smart Pointers
    1. 15.1. Box Points to Data on the Heap and Has a Known Size
  43. 15.2. The Deref Trait Allows Access to the Data Through a Reference
  44. 15.3. The Drop Trait Runs Code on Cleanup
  45. 15.4. Rc, the Reference Counted Smart Pointer
  46. 15.5. RefCell and the Interior Mutability Pattern
  47. 15.6. Creating Reference Cycles and Leaking Memory is Safe
  48. 16. Fearless Concurrency
    1. 16.1. Threads
  49. 16.2. Message Passing
  50. 16.3. Shared State
  51. 16.4. Extensible Concurrency: Sync and Send
  52. 17. Object Oriented Programming Features of Rust
    1. 17.1. Characteristics of Object-Oriented Languages
  53. 17.2. Using Trait Objects that Allow for Values of Different Types
  54. 17.3. Implementing an Object-Oriented Design Pattern
  55. 18. Patterns Match the Structure of Values
    1. 18.1. All the Places Patterns May be Used
  56. 18.2. Refutability: Whether a Pattern Might Fail to Match
  57. 18.3. All the Pattern Syntax
  58. 19. Advanced Features
    1. 19.1. Unsafe Rust
  59. 19.2. Advanced Lifetimes
  60. 19.3. Advanced Traits
  61. 19.4. Advanced Types
  62. 19.5. Advanced Functions & Closures
  63. 19.6. Macros
  64. 20. Final Project: Building a Multithreaded Web Server
    1. 20.1. A Single Threaded Web Server
  65. 20.2. Turning our Single Threaded Server into a Multithreaded Server
  66. 20.3. Graceful Shutdown and Cleanup
  67. 21. Appendix
    1. 21.1. A - Keywords
  68. 21.2. B - Operators and Symbols
  69. 21.3. C - Derivable Traits
  70. 21.4. D - Useful Development Tools
  71. 21.5. E - Editions
  72. 21.6. F - Translations
  73. 21.7. G - How Rust is Made and “Nightly Rust”

The Rust Programming Language

Method Syntax

Methods are similar to functions: they’re declared with the fn keyword and their name, they can have parameters and a return value, and they contain some code that is run when they’re called from somewhere else. However, methods are different from functions in that they’re defined within the context of a struct (or an enum or a trait object, which we cover in Chapters 6 and 17, respectively), and their first parameter is always self, which represents the instance of the struct the method is being called on.

Defining Methods

Let’s change the area function that has a Rectangle instance as a parameter and instead make an area method defined on the Rectangle struct, as shown in Listing 5-13:

Filename: src/main.rs

#[derive(Debug)]
struct Rectangle {
    width: u32,
    height: u32,
}

impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

fn main() {
    let rect1 = Rectangle { width: 30, height: 50 };

    println!(
        "The area of the rectangle is {} square pixels.",
        rect1.area()
    );
}

Listing 5-13: Defining an area method on theRectangle struct

To define the function within the context of Rectangle, we start an impl(implementation) block. Then we move the area function within the implcurly brackets and change the first (and in this case, only) parameter to beself in the signature and everywhere within the body. In main, where we called the area function and passed rect1 as an argument, we can instead use method syntax to call the area method on our Rectangle instance. The method syntax goes after an instance: we add a dot followed by the method name, parentheses, and any arguments.

In the signature for area, we use &self instead of rectangle: &Rectanglebecause Rust knows the type of self is Rectangle due to this method’s being inside the impl Rectangle context. Note that we still need to use the &before self, just as we did in &Rectangle. Methods can take ownership ofself, borrow self immutably as we’ve done here, or borrow self mutably, just as they can any other parameter.

We’ve chosen &self here for the same reason we used &Rectangle in the function version: we don’t want to take ownership, and we just want to read the data in the struct, not write to it. If we wanted to change the instance that we’ve called the method on as part of what the method does, we’d use &mut self as the first parameter. Having a method that takes ownership of the instance by using just self as the first parameter is rare; this technique is usually used when the method transforms self into something else and you want to prevent the caller from using the original instance after the transformation.

The main benefit of using methods instead of functions, in addition to using method syntax and not having to repeat the type of self in every method’s signature, is for organization. We’ve put all the things we can do with an instance of a type in one impl block rather than making future users of our code search for capabilities of Rectangle in various places in the library we provide.

Where’s the -> Operator?

In C and C++, two different operators are used for calling methods: you use. if you’re calling a method on the object directly and -> if you’re calling the method on a pointer to the object and need to dereference the pointer first. In other words, if object is a pointer,object->something() is similar to (*object).something().

Rust doesn’t have an equivalent to the -> operator; instead, Rust has a feature called automatic referencing and dereferencing. Calling methods is one of the few places in Rust that has this behavior.

Here’s how it works: when you call a method with object.something(), Rust automatically adds in &, &mut, or * so object matches the signature of the method. In other words, the following are the same:


# #![allow(unused_variables)]
#fn main() {
# #[derive(Debug,Copy,Clone)]
# struct Point {
#     x: f64,
#     y: f64,
# }
#
# impl Point {
#    fn distance(&self, other: &Point) -> f64 {
#        let x_squared = f64::powi(other.x - self.x, 2);
#        let y_squared = f64::powi(other.y - self.y, 2);
#
#        f64::sqrt(x_squared + y_squared)
#    }
# }
# let p1 = Point { x: 0.0, y: 0.0 };
# let p2 = Point { x: 5.0, y: 6.5 };
p1.distance(&p2);
(&p1).distance(&p2);
#}

The first one looks much cleaner. This automatic referencing behavior works because methods have a clear receiver—the type of self. Given the receiver and name of a method, Rust can figure out definitively whether the method is reading (&self), mutating (&mut self), or consuming (self). The fact that Rust makes borrowing implicit for method receivers is a big part of making ownership ergonomic in practice.

Methods with More Parameters

Let’s practice using methods by implementing a second method on the Rectanglestruct. This time, we want an instance of Rectangle to take another instance of Rectangle and return true if the second Rectangle can fit completely within self; otherwise it should return false. That is, we want to be able to write the program shown in Listing 5-14, once we’ve defined the can_holdmethod:

Filename: src/main.rs

fn main() {
    let rect1 = Rectangle { width: 30, height: 50 };
    let rect2 = Rectangle { width: 10, height: 40 };
    let rect3 = Rectangle { width: 60, height: 45 };

    println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
    println!("Can rect1 hold rect3? {}", rect1.can_hold(&rect3));
}

Listing 5-14: Using the as-yet-unwritten can_holdmethod

And the expected output would look like the following, because both dimensions of rect2 are smaller than the dimensions of rect1 but rect3 is wider thanrect1:

Can rect1 hold rect2? true
Can rect1 hold rect3? false

We know we want to define a method, so it will be within the impl Rectangleblock. The method name will be can_hold, and it will take an immutable borrow of another Rectangle as a parameter. We can tell what the type of the parameter will be by looking at the code that calls the method:rect1.can_hold(&rect2) passes in &rect2, which is an immutable borrow torect2, an instance of Rectangle. This makes sense because we only need to read rect2 (rather than write, which would mean we’d need a mutable borrow), and we want main to retain ownership of rect2 so we can use it again after calling the can_hold method. The return value of can_hold will be a Boolean, and the implementation will check whether the width and height ofself are both greater than the width and height of the other Rectangle, respectively. Let’s add the new can_hold method to the impl block from Listing 5-13, shown in Listing 5-15:

Filename: src/main.rs


# #![allow(unused_variables)]
#fn main() {
# #[derive(Debug)]
# struct Rectangle {
#     width: u32,
#     height: u32,
# }
#
impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }

    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }
}
#}

Listing 5-15: Implementing the can_hold method onRectangle that takes another Rectangle instance as a parameter

When we run this code with the main function in Listing 5-14, we’ll get our desired output. Methods can take multiple parameters that we add to the signature after the self parameter, and those parameters work just like parameters in functions.

Associated Functions

Another useful feature of impl blocks is that we’re allowed to define functions within impl blocks that don’t take self as a parameter. These are called associated functions because they’re associated with the struct. They’re still functions, not methods, because they don’t have an instance of the struct to work with. You’ve already used the String::from associated function.

Associated functions are often used for constructors that will return a new instance of the struct. For example, we could provide an associated function that would have one dimension parameter and use that as both width and height, thus making it easier to create a square Rectangle rather than having to specify the same value twice:

Filename: src/main.rs


# #![allow(unused_variables)]
#fn main() {
# #[derive(Debug)]
# struct Rectangle {
#     width: u32,
#     height: u32,
# }
#
impl Rectangle {
    fn square(size: u32) -> Rectangle {
        Rectangle { width: size, height: size }
    }
}
#}

To call this associated function, we use the :: syntax with the struct name;let sq = Rectangle::square(3); is an example. This function is namespaced by the struct: the :: syntax is used for both associated functions and namespaces created by modules. We’ll discuss modules in Chapter 7.

Multiple impl Blocks

Each struct is allowed to have multiple impl blocks. For example, Listing 5-15 is equivalent to the code shown in Listing 5-16, which has each method in its own impl block:


# #![allow(unused_variables)]
#fn main() {
# #[derive(Debug)]
# struct Rectangle {
#     width: u32,
#     height: u32,
# }
#
impl Rectangle {
    fn area(&self) -> u32 {
        self.width * self.height
    }
}

impl Rectangle {
    fn can_hold(&self, other: &Rectangle) -> bool {
        self.width > other.width && self.height > other.height
    }
}
#}

Listing 5-16: Rewriting Listing 5-15 using multiple implblocks

There’s no reason to separate these methods into multiple impl blocks here, but this is valid syntax. We’ll see a case in which multiple impl blocks are useful in Chapter 10 where we discuss generic types and traits.

Summary

Structs let you create custom types that are meaningful for your domain. By using structs, you can keep associated pieces of data connected to each other and name each piece to make your code clear. Methods let you specify the behavior that instances of your structs have, and associated functions let you namespace functionality that is particular to your struct without having an instance available.

But structs aren’t the only way you can create custom types: let’s turn to Rust’s enum feature to add another tool to your toolbox.