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

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.

source

fn origin(r: f64) -> Circle

Creates a Circle instance centered on the “origin” (x = 0, y = 0).

source

pub fn new(x: f64, y: f64, r: f64) -> Circle

Creates a Circle instance centered on specified x, y values.

source

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.

source

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).

source

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.

source

pub fn by_take_mut(self) -> f64

Returns the value of Circle.x, taking ownership of a mutable circle.

Trait Implementations§

source§

impl Debug for Circle

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.