Skip to main content

Deck

Struct Deck 

Source
pub struct Deck { /* private fields */ }
Expand description

The Deck represents a deck of zero or more cards. A default deck is 52 playing cards. Internally the deck consists of two stacks consisting of dealt and undealt cards. The dealt stack receives cards as they are dealt from the undealt stack.

The deck may be reset() to return it to its original state. A deck may be shuffle()’d to randomize its order. Shuffling uses a Knuth shuffle.

A deck can contain more than one card with the same rank / suit combination although by default it does not.

A deck cannot have more cards added or removed to it once it is created.

Implementations§

Source§

impl Deck

Source

pub fn new() -> Deck

Creates a new Deck containing the standard set of 52 cards

Examples found in repository?
examples/main.rs (line 4)
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}
Source

pub fn from_cards(cards: &[Card]) -> Deck

Creates a new Deck containing the specified cards

Source

pub fn undealt_count(&self) -> usize

Returns the number of remaining undealt cards in the Deck

Source

pub fn dealt_count(&self) -> usize

Returns the number of dealt cards in the Deck

Source

pub fn count(&self) -> usize

Returns the number of cards, dealt or undealt, within the Deck

Source

pub fn dealt_cards(&self) -> &[Card]

Returns the collection of dealt cards

Source

pub fn top_card(&self) -> Option<Card>

Tells you the top card (very next to be drawn) in the undealt deck without dealing it.

Source

pub fn bottom_card(&self) -> Option<Card>

Tells you the bottom card (very last to be drawn) in the undealt deck without dealing it.

Source

pub fn deal_one(&mut self) -> Result<Card, &'static str>

Deals the card from the undealt pile. If there are no cards left, the function will return an error.

Examples found in repository?
examples/main.rs (line 11)
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}
Source

pub fn deal(&mut self, numcards: usize) -> Vec<Card>

Deals one or more card from the undealt pile and returns them as an array.

Source

pub fn deal_to_hand(&mut self, hand: &mut Hand, numcards: usize) -> usize

Deals one or more card straight to the Hand. Returns the number of cards dealt.

Source

pub fn reset(&mut self)

Return the dealt cards back to the end of the undealt pile. Order is preserved according to the default order or the last shuffle.

Examples found in repository?
examples/main.rs (line 19)
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}
Source

pub fn reset_shuffle(&mut self)

Resets and shuffles the deck

Trait Implementations§

Source§

impl Cards for Deck

Source§

fn cards(&self) -> &[Card]

Return the cards as a slice
Source§

fn mut_cards(&mut self) -> &mut [Card]

Return the cards as a mutable slice
Source§

fn shuffle(&mut self)

Shuffle the cards into a random order
Source§

fn sort_suit_ascending_rank(&mut self)

Sort the cards by suit and then by rank (low to high)
Source§

fn sort_suit_descending_rank(&mut self)

Sorts the cards by suit and then by rank (high to low)
Source§

fn sort_descending_rank_suit(&mut self)

Sort the cards by rank (high to low) and then by suit
Source§

impl Clone for Deck

Source§

fn clone(&self) -> Deck

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

Auto Trait Implementations§

§

impl Freeze for Deck

§

impl RefUnwindSafe for Deck

§

impl Send for Deck

§

impl Sync for Deck

§

impl Unpin for Deck

§

impl UnsafeUnpin for Deck

§

impl UnwindSafe for Deck

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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V