Struct pokelab::pokemon::eevee::Eevee

source ·
pub struct Eevee {
    pub level: u8,
    pub health: u16,
    pub attack: u16,
    pub defense: u16,
}
Expand description

This type represents a basic Eevee pokemon. It has a level, as well as health, attack, and defense stats.

Something to note is that while it is possible to implement getters and setters for struct fields as if they were objects in OOP-paradigm languages, it is generally unnecessary for Rust. It can even get you into trouble with the borrow checker when dealing with fields that are references (we may talk about this when we get to lifetimes in week 7).

Marking a field as pub, coupled with the borrow checker, will give you very similar semantics as to normal getters and setters.

There are, of course, places where you do want these. And when we talk about traits in week 5, we will need to have them if we want to share behavior among types.

Fields§

§level: u8

For this part of the homework, level doesn’t actually represent anything important.

§health: u16§attack: u16§defense: u16

Implementations§

source§

impl Eevee

source

pub fn new() -> Self

Creates an Eevee with the following base stats:

  • level: 0
  • health: 100
  • attack: 55
  • defense: 20

Note: We 0-level because we’re programmers 😎

let new_eevee = Eevee::new();
assert_eq!(new_eevee.level, 0);
assert_eq!(new_eevee.health, 100);
assert_eq!(new_eevee.attack, 55);
assert_eq!(new_eevee.defense, 20);
source

pub fn take_damage(&mut self, damage: u16)

Deals damage amount of damage to the Eevee’s health.

let mut new_eevee = Eevee::new();
assert_eq!(new_eevee.health, 100);
assert_eq!(new_eevee.defense, 20);

new_eevee.take_damage(10);
assert_eq!(new_eevee.health, 100); // Not enough damage to overcome defense

new_eevee.take_damage(30);
assert_eq!(new_eevee.health, 90); // 30 - 20 = 10 damage taken

This function should panic with the message "Eevee fainted!" if it takes more damage than it has health. In other words, it should faint when it reaches 0 health.

source

pub fn evolve(self, evolution: ElementalStone) -> EvolvedEevee

Given an Elemental Stone, evolve the Eevee into an EvolvedEevee.

If given a stone that an Eevee cannot use to evolve, this function should panic with the message "Encountered a weird rock...".

let mut new_eevee = Eevee::new();

let vaporeon = new_eevee.evolve(ElementalStone::HydroStone);
assert!(matches!(vaporeon, EvolvedEevee::Vaporeon(_, _)));

Trait Implementations§

source§

impl Debug for Eevee

source§

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

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

impl PartialEq<Eevee> for Eevee

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Eevee

source§

impl StructuralEq for Eevee

source§

impl StructuralPartialEq for Eevee

Auto Trait Implementations§

§

impl RefUnwindSafe for Eevee

§

impl Send for Eevee

§

impl Sync for Eevee

§

impl Unpin for Eevee

§

impl UnwindSafe for Eevee

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.