[][src]Struct freecell::Card

pub struct Card {
    pub suit: Suit,
    pub rank: Rank,
}

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: Suitrank: Rank

Trait Implementations

impl Clone for Card[src]

impl Copy for Card[src]

impl Eq for Card[src]

impl PartialEq<Card> for Card[src]

impl Display for Card[src]

impl Debug for Card[src]

impl TryFrom<String> for Card[src]

type Error = String

The type returned in the event of a conversion error.

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

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"));

impl<'_> TryFrom<&'_ str> for Card[src]

type Error = String

The type returned in the event of a conversion error.

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

Converts a &str to a Card.

See the description of TryFrom<String> for details.

impl Hash for Card[src]

impl StructuralPartialEq for Card[src]

impl StructuralEq for Card[src]

Auto Trait Implementations

impl Send for Card

impl Sync for Card

impl Unpin for Card

impl UnwindSafe for Card

impl RefUnwindSafe for Card

Blanket Implementations

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> From<T> for T[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]