commit 84a49065afb3c04c20def46c31f926492ca7f8bc Author: Patrick MARIE Date: Fri Feb 5 17:40:29 2021 +0100 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..0516517 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,83 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ccac4b00700875e6a07c6cde370d44d32fa01c5a65cdd2fca6858c479d28bb3" + +[[package]] +name = "play-loto" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c026d7df8b298d90ccbbc5190bd04d85e159eaf5576caeacf8741da93ccbd2e5" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..73c7597 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "play-loto" +version = "0.1.0" +authors = ["Patrick MARIE "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.3" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1598750 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,46 @@ +use std::fmt; +use rand::Rng; +use std::iter::FromIterator; + +struct Numbers { + numbers: Vec +} + +fn get_numbers(numbers: &Vec, size: usize) -> Numbers { + let mut picked : Vec = vec![]; + let mut numbers = numbers.clone(); + + let mut rng = rand::thread_rng(); + + for _idx in 0..size { + let n : usize = rng.gen::() % numbers.len(); + picked.push(numbers.remove(n.into())); + } + picked.sort(); + + Numbers { numbers: picked } +} + +impl fmt::Display for Numbers { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let s = self.numbers.iter().fold(String::from(""), |mut s, x| { + if s != "" { + s.push_str(", "); + } + s.push_str(&format!("{}", x).to_string()); + s + }); + + write!(f, "{}", s) + } +} + +fn main() { + let numbers : Vec = Vec::from_iter(1..=50_u8); + let stars : Vec = Vec::from_iter(1..=12_u8); + + let picked_numbers = get_numbers(&numbers, 5); + let picked_stars = get_numbers(&stars, 2); + + println!("Numbers: {} / Stars: {}", picked_numbers, picked_stars); +}