struct Rectangle
{
width: i32,
height: i32,
}
impl Rectangle
{
fn area(&self) -> i32
{
self.width * self.height
}
fn can_hold(&self, other: &Rectangle) -> bool // &Rectangle confuses me
{
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));
}
fn can_hold(&self, other: &Rectangle) -> bool
This line kinda confuses me.
So in the main function, I understand that in this line println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
you are passing a reference of the rect2 struct. But in this associated function fn can_hold(&self, other: &Rectangle) -> bool
, is other: &Rectangle
telling the compiler that the other
variable is a &Rectangle
type? And the &self
is telling the compiler that use the contents of what is stored in rect1
?