pub struct Card {
pub rank: Rank,
pub suit: Suit,
}Expand description
A Card has a Rank and a Suit and represents a card from the normal 52-card
playing deck.
§Example
use deckofcards::{Card, Rank, Suit};
let card = Card::new(Rank::Jack, Suit::Hearts);Fields§
§rank: RankThe card’s Rank, e.g. Jack
suit: SuitThe card’s Suit, e.g. Hearts
Implementations§
Source§impl Card
impl Card
Sourcepub fn cmp_rank_then_suit(&self, other: &Card) -> Ordering
pub fn cmp_rank_then_suit(&self, other: &Card) -> Ordering
Compares by rank and then suit
Sourcepub fn cmp_desc_rank_then_suit(&self, other: &Card) -> Ordering
pub fn cmp_desc_rank_then_suit(&self, other: &Card) -> Ordering
Compares by descending rank and then suit
Sourcepub fn cmp_suit_then_rank(&self, other: &Card) -> Ordering
pub fn cmp_suit_then_rank(&self, other: &Card) -> Ordering
Compares by suit and then rank
Sourcepub fn cmp_suit_then_desc_rank(&self, other: &Card) -> Ordering
pub fn cmp_suit_then_desc_rank(&self, other: &Card) -> Ordering
Compares by suit and then rank
Sourcepub fn from_str(s: &str) -> Result<Card, &'static str>
pub fn from_str(s: &str) -> Result<Card, &'static str>
Creates a card from a string such, e.g. “AS” returns Ace of Spades
Sourcepub fn to_str(&self) -> String
pub fn to_str(&self) -> String
Turns the card into a short string consisting of rank, suit, e.g. “AS”
Sourcepub fn name(&self) -> String
pub fn name(&self) -> String
Returns an English formatted name of the card, e.g. “Ace of Spades”
Examples found in repository?
examples/main.rs (line 12)
3fn main() {
4 let mut deck = Deck::new();
5
6 // Shuffle the deck
7 deck.shuffle();
8
9 // Deal a card
10 for _ in 0..10 {
11 if let Ok(card) = deck.deal_one() {
12 println!("You dealt a {}", card.name());
13 } else {
14 panic!("We should have enough cards for this not to happen")
15 }
16 }
17
18 // Put dealt cards back onto the deck
19 deck.reset();
20}Sourcepub fn ordinal(&self) -> usize
pub fn ordinal(&self) -> usize
Returns an ordinal for the card which is a unique number which can be used for indexing
Sourcepub fn is_diamonds(&self) -> bool
pub fn is_diamonds(&self) -> bool
Tests if the card is Diamonds
Sourcepub fn all_cards() -> &'static [Card]
pub fn all_cards() -> &'static [Card]
Returns an array slice containing all the cards in a standard 52-card deck
pub fn iterator() -> Iter<'static, Card>
Trait Implementations§
Source§impl AddAssign<Card> for Hand
impl AddAssign<Card> for Hand
Source§fn add_assign(&mut self, rhs: Card)
fn add_assign(&mut self, rhs: Card)
Performs the
+= operation. Read moreSource§impl Ord for Card
impl Ord for Card
Source§impl PartialOrd for Card
impl PartialOrd for Card
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 UnsafeUnpin 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