Struct Speed

Source
pub struct Speed(/* private fields */);
Expand description

Represents both the movement and rotation speed of the turtle.

§Creating Speeds

You can create a Speed value by converting either strings or numbers.

// This value is of type `Speed` and it is converted from an `i32`
let speed: Speed = 1.into();

// This value is of type `Speed` and it is converted from a `&str`
let slowest_speed: Speed = "slowest".into();

There is no need to call .into() when passing a speed into the set_speed method. See the set_speed method for more information about how that works.

let mut turtle = Turtle::new();
turtle.set_speed(22); // Same as `turtle.set_speed(22.into())`

The minimum speed value is 1 and the maximum speed value (currently) is 25.

Trying to set the speed to a value out of that range will cause a panic.

let mut turtle = Turtle::new();
turtle.set_speed(26); // panic!

While the minimum speed will not change, the maximum speed may grow larger if the need arises. That is why we chose to panic for invalid speeds instead of defaulting to another value.

§String Conversion

Strings are converted as follows:

StringValue
"slowest"1
"slower"5
"slow"8
"normal"10
"fast"12
"faster"15
"instant"see below

You can use strings to create Speed values in the same way numbers were used above. Each of the following is an equivalent way to set the speed to 5:

turtle.set_speed(5);
turtle.set_speed("slower");
turtle.set_speed(Speed::from(5)); // Not recommended!
turtle.set_speed(Speed::from("slower")); // Not recommended!

§Instant

There is one special speed value "instant" which makes it so that movement and rotation are not animated at all. Instead, the turtle immediately moves and rotates to the position that you directed it to. It will still draw along the way if its pen is down.

let mut turtle = Turtle::new();
turtle.set_speed("instant");
turtle.forward(100.0); // A line will be drawn instantly!

§Comparing Speed Values

Speed values can be compared for equality with i32 values. This is a little more convenient than converting the i32 to Speed every time you want to make a comparison.

let speed: Speed = 1.into();
// Speed values can be compared to integers
assert_eq!(speed, 1);
// This is equivalent to the following
assert_eq!(speed, Speed::from(1));

// This value is of type `Speed` and it is converted from a `&str`
let speed: Speed = "slowest".into();
// Speed values can be compared to other speed values
assert_eq!(speed, Speed::from("slowest"));
// This is equivalent to the following since the slowest speed is 1
assert_eq!(speed, 1);

You can use the <, <=, ==, >=, > with Speed values and i32 values (or other Speed values).

let turtle = Turtle::new();
let speed = turtle.speed();
if speed == 12 && speed >= 5 && speed < Speed::instant() {
    println!("Super fast!!");
}
// This is equivalent, but requires more typing
if speed == Speed::from(12) && speed >= Speed::from(5) && speed < Speed::from("instant") {
    println!("Super fast!!");
}

Notice that you can compare Speed values to numeric values, but not the other way around.

let speed: Speed = 7.into();
if 8 > speed { // This doesn't make sense and won't compile!
    // ...
}

To check if a speed is instant, use the is_instant() method or compare the speed to Speed::instant().

let speed = Speed::instant();
if speed.is_instant() {
    println!("Instant!!");
}

Implementations§

Source§

impl Speed

Source

pub fn instant() -> Self

Returns the speed value that will make the turtle move and rotate instantly. This means that instead of the turtle’s movements being animated, it will directly move to wherever you direct it to go.

let mut turtle = Turtle::new();
turtle.set_speed(Speed::instant());
turtle.forward(100.0); // A line will be drawn instantly!

Whenever possible, you should prefer to use the string "instant" instead of calling this method.

turtle.set_speed("instant"); // equivalent to typing `Speed::instant()`
Source

pub fn is_instant(self) -> bool

Returns true if this speed is the same as Speed::instant()

use turtle::{Speed};
let speed: Speed = "instant".into();
assert!(speed.is_instant());

let speed = Speed::instant();
assert!(speed.is_instant());

Trait Implementations§

Source§

impl Clone for Speed

Source§

fn clone(&self) -> Speed

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Speed

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Speed

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Speed

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a str> for Speed

Source§

fn from(s: &'a str) -> Self

Converts to this type from the input type.
Source§

impl From<f64> for Speed

Source§

fn from(n: f64) -> Self

Converts to this type from the input type.
Source§

impl From<i32> for Speed

Source§

fn from(n: i32) -> Self

Converts to this type from the input type.
Source§

impl Hash for Speed

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for Speed

Source§

fn cmp(&self, other: &Speed) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq<i32> for Speed

Source§

fn eq(&self, other: &i32) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq for Speed

Source§

fn eq(&self, other: &Speed) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd<i32> for Speed

Source§

fn partial_cmp(&self, other: &i32) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PartialOrd for Speed

Source§

fn partial_cmp(&self, other: &Speed) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Random for Speed

Source§

fn random() -> Self

Generates a random speed within the valid range of speed levels

Source§

impl<B: Into<Speed>> RandomRange<B> for Speed

Source§

fn random_range(low: B, high: B) -> Self

Generates a random difficulty level within the given range, not including instant.

§Panics

Panics if either bound could result in a value outside the valid range of speed levels or if low > high. Also panics if either bound is Speed::instant().

Source§

impl Serialize for Speed

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Speed

Source§

impl Eq for Speed

Source§

impl StructuralPartialEq for Speed

Auto Trait Implementations§

§

impl Freeze for Speed

§

impl RefUnwindSafe for Speed

§

impl Send for Speed

§

impl Sync for Speed

§

impl Unpin for Speed

§

impl UnwindSafe for Speed

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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 T
where 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> SetParameter for T

Source§

fn set<T>(&mut self, value: T) -> <T as Parameter<Self>>::Result
where T: Parameter<Self>,

Sets value as a parameter of self.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

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 T
where U: TryFrom<T>,

Source§

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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,