Rust Katas

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) } } ...

March 1, 2018 · 1 min · 180 words · Micha Kops

Postgres Snippets

Get size of a table SELECT pg_size_pretty(pg_total_relation_size('schemaname.tablename')); Select unique combination of fields / tuples SELECT DISTINCT ON(field1, field2) field1, field2 FROM thetable Select rows where a combination of fields is not unique SELECT columnA, columnB, count(*) AS count FROM thetable GROUP BY columnA, columnB HAVING count(*) > 1 Search for rows with array containing value Assuming, the field appointments has the type date[] SELECT * FROM mtable WHERE appointments @> ARRAY['2023-09-19'::date] ...

March 1, 2010 · 8 min · 1655 words · Micha Kops