Section 4.15 Method Calls example, book 2015.12.18


#1

I added the last six lines of code to your book example, shown further below. They do not work. Please provide example calls to fn reference, fn mutable reference and fn takes_ownership. Thanks.

fn main() {
    struct Circle {
        x: f64,
        y: f64,
        radius: f64,
    }
    
    impl Circle {
        fn reference(&self) {
           println!("taking self by reference!");
        }
    }
    
    impl Circle {
        fn mutable_reference(&mut self) {
           println!("taking self by mutable reference!");
        }
    }
    
    impl Circle {
        fn takes_ownership(self) {
           println!("taking ownership of self!");
        }
    }

    let a = &Circle { x: 0.0, y: 0.0, radius: 2.0 };
    let b = Circle { x: 0.0, y: 0.0, radius: 2.0 };
    let c = &mut Circle { x: 0.0, y: 0.0, radius: 2.0 };
    
    println!("{}", a.reference());
    println!("{}", b.takes_ownership());
    println!("{}", c.mutable_reference());

}

#2

Your code compiles except for the println! calls, because the return type of each of your methods is the unit type () which doesn’t implement the Display trait.

If you remove the println!() then the code works:

    a.reference();
    b.takes_ownership();
    c.mutable_reference();

#3

Got it. Thanks