I’m just learning Rust .. code = crap :D |
Lotto Generator
Version 2
src/main.rs
use rand::Rng;
use std::fmt;
fn main() {
let lotto = Game {
name: String::from("Lotto 6aus49"),
take: 6,
lower: 1,
upper: 49,
};
println!("Playing {lotto}");
lotto.print_result();
}
struct Game {
name: String,
take: usize,
lower: u32,
upper: u32,
}
impl Game {
fn print_result(&self){
let mut rng = rand::thread_rng();
let mut result:Vec<u32> = Vec::new();
while result.len() < self.take {
let num = rng.gen_range(self.lower..self.upper);
if !result.contains(&num) {
result.push(num);
}
}
result.sort();
println!("Result: {result:?}");
}
}
impl fmt::Display for Game {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} ({} from {} to {}))", self.name, self.take, self.lower, self.upper)
}
}
Version 1
src/main.rs
use rand::Rng;
fn main() {
let mut rng = rand::thread_rng();
let lotto = Game {
name: String::from("Lotto 6aus49"),
take: 6,
lower: 1,
upper: 49,
};
println!("Playing {lotto:?}");
let mut result:Vec<u32> = Vec::new();
while result.len() < lotto.take {
let num = rng.gen_range(lotto.lower..lotto.upper);
if !result.contains(&num) {
result.push(num);
}
}
result.sort();
println!("Result: {result:?}");
}
#[derive(Debug)]
struct Game {
name: String,
take: usize,
lower: u32,
upper: u32,
}