Struct show_notes::e002::Circle
source · pub struct Circle {
pub x: f64,
pub y: f64,
pub r: f64,
}
Expand description
This struct is simple but useful to see how borrowing and moving work.
Fields§
§x: f64
X position of the circle’s origin.
y: f64
Y position of the circle’s origin
r: f64
Radius of the circle
Implementations§
source§impl Circle
impl Circle
Implement some methods on the Circle
.
This lets use demonstrate both how methods work in general and specifically how they interact with the idea of ownership.
sourcepub fn new(x: f64, y: f64, r: f64) -> Circle
pub fn new(x: f64, y: f64, r: f64) -> Circle
Creates a Circle
instance centered on specified x, y values.
sourcepub fn x_by_ref(&self) -> f64
pub fn x_by_ref(&self) -> f64
Returns the value of Circle.x
, borrowing an immutable reference to
the circle to do it.
Because the reference is immutable, if you tried to do this—
self.x = 10;
—the compiler would not allow it.
sourcepub fn x_by_mut_ref(&mut self) -> f64
pub fn x_by_mut_ref(&mut self) -> f64
Returns the value of Circle.x
, borrowing a mutable reference to the
circle and changing the value (demonstrating a situation in which you
would want to use a mutable rather than immutable reference).
sourcepub fn by_take(self) -> f64
pub fn by_take(self) -> f64
Returns the value of Circle.x
, taking ownership of the circle. As a
result of the change in ownership, the circle goes out of scope after
the method returns, so the circle instance will be inaccessible after
that.
Note that the item is taken as immutable, so attempting to change the internals will still fail. Ownership is orthogonal to immutability.
sourcepub fn by_take_mut(self) -> f64
pub fn by_take_mut(self) -> f64
Returns the value of Circle.x
, taking ownership of a mutable circle.