Struct Card

Source
pub struct Card {
    pub suit: Suit,
    pub rank: Rank,
}
Expand description

Represents one card in the game.

Cards can be converted from Strings or &strs. The formats for this are the same formats used by Display and Debug. See the descriptions for TryFrom<String> and TryFrom<&str> below for details.

§Examples

Parsing cards from strings:

use std::convert::TryFrom;
use freecell::{ACE, JACK, KING, QUEEN};
use freecell::Suit::{Club, Diamond, Heart, Spade};

// Short representation used by Debug
assert_eq!(Ok(Card { suit: Diamond, rank: ACE }), Card::try_from("AD"));
assert_eq!(Ok(Card { suit: Club, rank: 4 }), Card::try_from("4C"));
assert_eq!(Ok(Card { suit: Diamond, rank: 10 }), Card::try_from("TD"));
assert_eq!(Ok(Card { suit: Club, rank: JACK }), Card::try_from("jc"));
assert_eq!(Ok(Card { suit: Spade, rank: QUEEN }), Card::try_from("qS"));
assert_eq!(Ok(Card { suit: Heart, rank: KING }), Card::try_from("Kh"));

// Long representation used by Display
assert_eq!(Ok(Card { suit: Diamond, rank: JACK }), Card::try_from("Jack of Diamonds"));
assert_eq!(Ok(Card { suit: Club, rank: 10 }), Card::try_from("10 oF cLuBs"));

A formatted card can be converted back to the original card:

let ace_of_spades = Card { suit: Spade, rank: ACE };
// Formatted using Display
assert_eq!(Ok(ace_of_spades), Card::try_from(ace_of_spades.to_string()));
// Formatted using Debug
assert_eq!(Ok(ace_of_spades), Card::try_from(format!("{:?}", ace_of_spades)));

Fields§

§suit: Suit§rank: Rank

Trait Implementations§

Source§

impl Clone for Card

Source§

fn clone(&self) -> Card

Returns a duplicate 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 Card

Source§

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

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

impl Display for Card

Source§

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

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

impl Hash for Card

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 PartialEq for Card

Source§

fn eq(&self, other: &Card) -> 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 TryFrom<&str> for Card

Source§

fn try_from(string: &str) -> Result<Card, Self::Error>

Converts a &str to a Card.

See the description of TryFrom<String> for details.

Source§

type Error = String

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

impl TryFrom<String> for Card

Source§

fn try_from(string: String) -> Result<Card, Self::Error>

Converts a String to a Card.

The String must follow one of two formats. Both formats are case-insensitive.

§The short format used by Debug

The card is represented by a string of two characters. The first character denotes the card’s rank:

  • ‘A’ or ‘1’ - Ace
  • ‘2’ - 2
  • ‘9’ - 9
  • ‘T’ - 10
  • ‘J’ - Jack
  • ‘Q’ - Queen
  • ‘K’ - King

The second character denotes the suit:

  • ‘C’ - Club
  • ‘S’ - Spade
  • ‘H’ - Heart
  • ‘D’ - Diamond
§Examples
use freecell::{ACE, JACK, KING, QUEEN};
use freecell::Suit::{Club, Diamond, Heart, Spade};

assert_eq!(Ok(Card { suit: Diamond, rank: ACE }), Card::try_from("AD"));
assert_eq!(Ok(Card { suit: Club, rank: 4 }), Card::try_from("4C"));
assert_eq!(Ok(Card { suit: Diamond, rank: 10 }), Card::try_from("TD"));
assert_eq!(Ok(Card { suit: Club, rank: JACK }), Card::try_from("jc"));
assert_eq!(Ok(Card { suit: Spade, rank: QUEEN }), Card::try_from("qS"));
assert_eq!(Ok(Card { suit: Heart, rank: KING }), Card::try_from("Kh"));
§The long format used by Display

The card is represented by a string of the form “<rank> of <suit>s”, where <rank> can be the rank’s number or its name.

§Examples
use freecell::{JACK, QUEEN};
use freecell::Suit::{Club, Diamond, Spade};

assert_eq!(Ok(Card { suit: Diamond, rank: JACK }), Card::try_from("Jack of Diamonds"));
assert_eq!(Ok(Card { suit: Club, rank: 3 }), Card::try_from("3 of clubs"));
assert_eq!(Ok(Card { suit: Spade, rank: QUEEN }), Card::try_from("12 oF sPaDeS"));
Source§

type Error = String

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

impl Copy for Card

Source§

impl Eq for Card

Source§

impl StructuralPartialEq for Card

Auto Trait Implementations§

§

impl Freeze for Card

§

impl RefUnwindSafe for Card

§

impl Send for Card

§

impl Sync for Card

§

impl Unpin for Card

§

impl UnwindSafe for Card

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