Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding the 'self' parameter in the context of trait implementations

Tags:

self

rust

traits

When implementing a trait, we often use the keyword self, a sample is as follows. I want to understand the representation of the many uses of self in this code sample.

struct Circle {
    x: f64,
    y: f64,
    radius: f64,
}

trait HasArea {
    fn area(&self) -> f64;          // first self: &self is equivalent to &HasArea
}

impl HasArea for Circle {
    fn area(&self) -> f64 {         //second self: &self is equivalent to &Circle
        std::f64::consts::PI * (self.radius * self.radius) // third:self
    }
}

My understanding is:

  1. The first self: &self is equivalent to &HasArea.
  2. The second self: &self is equivalent to &Circle.
  3. Is the third self representing Circle? If so, if self.radius was used twice, will that cause a move problem?

Additionally, more examples to show the different usage of the self keyword in varying context would be greatly appreciated.

like image 537
enaJ Avatar asked Nov 01 '16 06:11

enaJ


1 Answers

You're mostly right.

The way I think of it is that in a method signature, self is a shorthand:

impl S {
    fn foo(self) {}      // equivalent to fn foo(self: S)
    fn foo(&self) {}     // equivalent to fn foo(self: &S)
    fn foo(&mut self) {} // equivalent to fn foo(self: &mut S)
}

It's not actually equivalent since self is a keyword and there are some special rules (for example for lifetime elision), but it's pretty close.

Back to your example:

impl HasArea for Circle {
    fn area(&self) -> f64 {   // like fn area(self: &Circle) -> ... 
        std::f64::consts::PI * (self.radius * self.radius)
    }
}

The self in the body is of type &Circle. You can't move out of a reference, so self.radius can't be a move even once. In this case radius implements Copy, so it's just copied out instead of moved. If it were a more complex type which didn't implement Copy then this would be an error.

like image 77
Chris Emerson Avatar answered Sep 28 '22 16:09

Chris Emerson