Method Syntax
Similar and Difference
Methods are similar to functions:
- we declare them with the 
fnkeyword and a name - they can have parameters and a return value
 - and they contain some code that’s run when the method is called from somewhere else.
 
Unlike functions:
- methods are 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.
Listing 5-13: Defining an area method on the Rectangle struct
#[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() ); }
To define the function within the context of Rectangle:
- we start an 
impl(implementation) block forRectangle. - Everything within this 
implblock will be associated with theRectangletype. - Then we move the 
areafunction within theimplcurly brackets and change the first (and in this case, only) parameter to beselfin the signature and everywhere within the body. - In 
main, where we called theareafunction and passedrect1as an argument, - we can instead use method syntax to call the 
areamethod on ourRectangleinstance. - The method syntax goes after an instance: we add a dot followed by the method name, parentheses, and any arguments.
 
rare self, &self, &mut self and Self
- Instead:
In the signature for 
area, we use&selfinstead ofrectangle: &Rectangle. - The 
&selfis actually short forself: &Self. - Within an 
implblock, the typeSelfis an alias for the type that theimplblock is for, just like class in python. - Methods must
have a parameter named 
selfof typeSelffor their first parameter, so Rust lets you abbreviate this with only the nameselfin the first parameter spot. 
Note that we still need to use the
&in front of theselfshorthand to indicate this method borrows theSelfinstance, just as we did inrectangle: &Rectangle.
- rare self: take ownership or borrow immutably
 
- Methods can take ownership of 
self, borrowselfimmutably as we’ve done here, or borrowselfmutably, just as they can any other parameter. - Having a method that takes ownership of the
instance by using just 
selfas the first parameter is rare; - this technique is
 
- &self: don’t want to take ownership
 
- We’ve chosen 
&selfhere for the same reason we used&Rectanglein 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.
 
- &mut self: change the instance
 
- 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 selfas the first parameter. usually used when the method transformsselfinto something else and you want to prevent the caller from using the original instance after the transformation. 
The main reason for using methods instead of functions, in addition to providing method syntax and not having to repeat the type of
selfin 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.
Note that we can choose to give a method the same name as one of the struct’s fields.
For example, we can define a method on Rectangle also named width:
#[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { fn width(&self) -> bool { self.width > 0 } } fn main() { let rect1 = Rectangle { width: 30, height: 50, }; if rect1.width() { println!("The rectangle has a nonzero width; it is {}", rect1.width); } }
- 
Here, we’re choosing to make the
widthmethod returntrueif the value in the instance’swidthfield is greater than 0, andfalseif the value is 0 - 
we can use a field within a method of the same name for any purpose.
 - 
In
main, when we followrect1.widthwith parentheses, Rust knows we mean the methodwidth. When we don’t use parentheses, Rust knows we mean the fieldwidth. 
Often, but not always, when we give methods with the same name as a field we want it to only return the value in the field and do nothing else. Methods like this are called getters, and Rust does not implement them automatically for struct fields as some other languages do.
Getters are useful because you can make the field private but the method public and thus enable read-only access to that field as part of the type’s public API. We will be discussing what public and private are and how to designate a field or method as public or private in Chapter 7.
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 
objectis 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*soobjectmatches the signature of the method. 
In other words, the following are the same:
#![allow(unused)] 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 Rectangle
struct.
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 (the first Rectangle);
otherwise it should return false.
That is, once we’ve defined the can_hold method, we want to be able to write the
program shown in Listing 5-14.
Listing 5-14: Using the as-yet-unwritten can_hold method
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));
}
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 than
rect1:
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 anotherRectangleas 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 ofRectangle.- This makes sense because we only need to
read 
rect2(rather than write, which would mean we’d need a mutable borrow), and we wantmainto retain ownership ofrect2so we can use it again after calling thecan_holdmethod. - The return value of 
can_holdwill be a Boolean, and the implementation will check whether the width and height ofselfare both greater than the width and height of the otherRectangle, respectively. 
Let’s add the new can_hold method to the impl block from
Listing 5-13, shown in Listing 5-15.
Listing 5-15: Implementing the can_hold method on Rectangle that takes another Rectangle instance as a parameter
#[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 } } 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)); }
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
selfparameter, and those parameters work just like parameters in functions.
Associated Functions Without self: not method, just like static method
All functions defined within an
implblock are called associated functions because they’re associated with the type named after theimpl.
We can define
associated functions that don’t have self as their first parameter (and thus
are not methods) because they don’t need an instance of the type to work with.
not dot, use “::”: We’ve already used one function like this: the
String::fromfunction that’s defined on theStringtype.
Self keyword used in constructors
Associated functions that aren’t methods are often used for constructors that will return a new instance of the struct:
- These are often called 
new, butnewisn’t a special name and isn’t built into the language. 
For example, we could choose to provide an associated function named square 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:
#[derive(Debug)] struct Rectangle { width: u32, height: u32, } impl Rectangle { fn square(size: u32) -> Self { Self { width: size, height: size, } } } fn main() { let sq = Rectangle::square(3); }
The
Selfkeywords in the return type and in the body of the function are aliases for the type that appears after theimplkeyword, which in this case isRectangle.
- 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: Progressive constraints as needed
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.
Listing 5-16: Rewriting Listing 5-15 using multiple impl blocks
#[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 } } 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)); }
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.
 - 
In
implblocks, you can define functions that are associated with your type, and methods are a kind of associated function that let you specify the behavior that instances of your structs have. 
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.