Pure Programmer
Blue Matrix


Cluster Map

Project: Shuffle Deck

Write a function that takes a list of integers and then shuffles them in place into a random order. You can test the function by passing in a list of the integers 0-51. Then print the shuffled array as playing cards A, 2-10, J, Q, K and suits Clubs, Spades, Diamonds and Hearts by writing a function that converts an integer 0-51 into a card value and suit. Use appropriate assertions.

The shuffling algorithm to use is to randomly select on element of the array. Swap it with the first element of the list. Then randomly select an element from the elements after the first. Swap that random element with the second in the list. Continue until all the elmement have been swapped.

Output
$ rustc ShuffleDeck.rs error[E0308]: mismatched types --> ShuffleDeck.rs:9:19 | 9 | let size:isize = list.len(); | ----- ^^^^^^^^^^ expected `isize`, found `usize` | | | expected due to this | help: you can convert a `usize` to an `isize` and panic if the converted value doesn't fit | 9 | let size:isize = list.len().try_into().unwrap(); | ++++++++++++++++++++ error[E0425]: cannot find function `irandom` in this scope --> ShuffleDeck.rs:14:24 | 14 | let pos:isize = i + irandom(remaining); | ^^^^^^^ not found in this scope error[E0277]: the type `[isize]` cannot be indexed by `isize` --> ShuffleDeck.rs:15:26 | 15 | let temp:isize = list[i]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> ShuffleDeck.rs:16:9 | 16 | list[i] = list[pos]; | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> ShuffleDeck.rs:16:19 | 16 | list[i] = list[pos]; | ^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0277]: the type `[isize]` cannot be indexed by `isize` --> ShuffleDeck.rs:17:9 | 17 | list[pos] = temp; | ^^^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error[E0425]: cannot find function `substr` in this scope --> ShuffleDeck.rs:30:23 | 30 | let suitStr:String = substr(suits,suit,suit + 1); | ^^^^^^ not found in this scope error[E0425]: cannot find function `substr` in this scope --> ShuffleDeck.rs:31:28 | 31 | let mut valueStr:String = substr(values,value,value + 1); | ^^^^^^ not found in this scope error[E0277]: the type `[isize]` cannot be indexed by `isize` --> ShuffleDeck.rs:53:40 | 53 | println!("{}", convertInt2Card(deck[i])); | ^ slice indices are of type `usize` or ranges of `usize` | = help: the trait `SliceIndex<[isize]>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `Vec<isize>` to implement `Index<isize>` error: aborting due to 9 previous errors Some errors have detailed explanations: E0277, E0308, E0425. For more information about an error, try `rustc --explain E0277`.

Solution