pub struct Card {
pub suit: Suit,
pub rank: Rank,
}
Expand description
Represents one card in the game.
Cards can be converted from Strings or &str
s.
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 TryFrom<String> for Card
impl TryFrom<String> for Card
Source§fn try_from(string: String) -> Result<Card, Self::Error>
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"));
impl Copy for Card
impl Eq for Card
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more