From 1169f40d7124efc6f4aed976c22f305c19a0ecd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20Sj=C3=B6berg?= Date: Wed, 22 Nov 2023 17:56:55 +0800 Subject: [PATCH] moved to playground --- _rust/advanced-rust-in-60-seconds/Cargo.toml | 8 -- _rust/advanced-rust-in-60-seconds/src/main.rs | 49 -------- _rust/rust-in-60-seconds/Cargo.toml | 8 -- _rust/rust-in-60-seconds/src/main.rs | 112 ------------------ _rust/rust-macros/Cargo.toml | 8 -- _rust/rust-macros/src/main.rs | 35 ------ 6 files changed, 220 deletions(-) delete mode 100644 _rust/advanced-rust-in-60-seconds/Cargo.toml delete mode 100644 _rust/advanced-rust-in-60-seconds/src/main.rs delete mode 100644 _rust/rust-in-60-seconds/Cargo.toml delete mode 100644 _rust/rust-in-60-seconds/src/main.rs delete mode 100644 _rust/rust-macros/Cargo.toml delete mode 100644 _rust/rust-macros/src/main.rs diff --git a/_rust/advanced-rust-in-60-seconds/Cargo.toml b/_rust/advanced-rust-in-60-seconds/Cargo.toml deleted file mode 100644 index 1971fcb..0000000 --- a/_rust/advanced-rust-in-60-seconds/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "advanced_rust_in_60_seconds" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/_rust/advanced-rust-in-60-seconds/src/main.rs b/_rust/advanced-rust-in-60-seconds/src/main.rs deleted file mode 100644 index ecfd2e5..0000000 --- a/_rust/advanced-rust-in-60-seconds/src/main.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::thread; -use std::sync::{ Arc, Mutex }; -use std::sync::mpsc; // multiple producer, single consumer -// use std::ops::Add; // + operator as trait - -// custom trait to extend behaviour of Add operator -trait BetterAdd { - type Output; - fn better_add(self, rhs: RHS) -> Self::Output; -} - -impl BetterAdd for f32 { - type Output = f32; - fn better_add(self, rhs: i32) -> f32 { - self + rhs as f32 - } -} - -fn main() { - let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; - - // channel for send an receive - let (tx, rx) = mpsc::channel(); - - // mutexed accumulator for sum - let sum = Arc::new(Mutex::new(0.0)); - - // spawn threads - for chunk in numbers.chunks(3) { - let tx = tx.clone(); - let sum = Arc::clone(&sum); - - // not working? - thread::spawn(move || { - let partial_sum: f32 = chunk.iter().map(|&x| x).fold(0.0, |acc, x| acc.better_add(x)); - let mut sum = sum.lock().unwrap(); - *sum = sum.better_add(partial_sum); - tx.send(()).unwrap(); - }); - } - - // wait for threads to finish - for _ in 0..numbers.len() / 3 { - rx.recv().unwrap(); - } - - // print result - println!("Sum is {}", *sum.lock().unwrap()); -} diff --git a/_rust/rust-in-60-seconds/Cargo.toml b/_rust/rust-in-60-seconds/Cargo.toml deleted file mode 100644 index ce28fdd..0000000 --- a/_rust/rust-in-60-seconds/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "rust_in_60_seconds" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/_rust/rust-in-60-seconds/src/main.rs b/_rust/rust-in-60-seconds/src/main.rs deleted file mode 100644 index 6ab652c..0000000 --- a/_rust/rust-in-60-seconds/src/main.rs +++ /dev/null @@ -1,112 +0,0 @@ -/* - cargo new path/to/folder -*/ - -use std::io; -use std::collections::HashMap; - -// structs -struct Person { - name: String, - age: u32, // unsigned 32 bit integer -} - -// enums -#[allow(dead_code)] // suppress warnings for unused variants -enum Color { - Red, - Green, - Blue, -} - -// traits and implementations -impl Person { - fn greet(&self) { - println!("Hello, my name is {} and my age is {}.", self.name, self.age); - } -} - -fn main() { - println!("Rust in 60 seconds!"); - - // variables and mutability - let mut counter = 0; - counter += 1; - - // control flow - if counter > 0 { - println!("Counter is greater than 0."); - } else { - println!("Counter is less than 0."); - } - - // loops - for i in 0..5 { - println!("Iteration {}", i); - } - - // pattern matching - let number = 42; - match number { - 0 => println!("It's zero."), - 1..=100 => println!("It's between 1 and 100."), - _ => println!("It's something else."), - } - - // option and result - let result: Result = Ok(42); - match result { - Ok(value) => println!("Result is {}", value), - Err(err) => println!("Error {}", err), - } - - // structs and methods - let person = Person { - name: String::from("Alice"), - age: 42, - }; - person.greet(); - - // vectors - let mut numbers = vec![1, 2, 3]; - numbers.push(4); - - // hashmap - let mut scores = HashMap::new(); - scores.insert(String::from("Alice"), 100); - scores.insert(String::from("Bob"), 200); - - // enums - let color = Color::Red; - match color { - Color::Red => println!("It's red."), - Color::Green => println!("It's green."), - Color::Blue => println!("It's blue."), - } - - // file io - let mut input = String::new(); - io::stdin().read_line(&mut input).expect("Failed to read line."); - println!("You typed: {}", input.trim()); - // You typed: hello rust - - // ownership - let value = String::from("ABCDEF"); - take_ownership(value); // .clone() to avoid moving value - // println!("Value: {}", value); // compile-time error - - // borrowing - let message = String::from("This is my message."); - let len = calculate_length(&message); // &message is a reference to message - println!("Length of message is {}.", len); - // Length of message is 19. -} - -fn take_ownership(value: String) { - println!("Value: {}", value); - // nothing is returned at the end of this function -} - -fn calculate_length(s: &String) -> usize { - s.len() // s is returned at the end of this function -} diff --git a/_rust/rust-macros/Cargo.toml b/_rust/rust-macros/Cargo.toml deleted file mode 100644 index 376899f..0000000 --- a/_rust/rust-macros/Cargo.toml +++ /dev/null @@ -1,8 +0,0 @@ -[package] -name = "rust_macros" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] diff --git a/_rust/rust-macros/src/main.rs b/_rust/rust-macros/src/main.rs deleted file mode 100644 index cb10c1c..0000000 --- a/_rust/rust-macros/src/main.rs +++ /dev/null @@ -1,35 +0,0 @@ -// macro to generate getter and setter for struct -macro_rules! generate_getter_setter { - ($struct_name:ident, { $($field_name:ident: $field_type:ty),* }) => { - impl $struct_name { - $( - fn $field_name(&self) -> $field_type { - self.$field_name // return - } - - // not valid rust? - fn set_$field_name(&mut self, value: $field_type) { - self.$field_name = value; // assign - } - )* - } - }; -} - -struct Person { - Name: String, - Age: u32, -} - -generate_getter_setter!(Person, { Name: String, Age: u32 }); - -fn main() { - let mut person = Person { - Name: String::from("Alice"), - Age: 42, - }; - - println!("Name: {}", person.Name()); - person.set_name(String::from("Bob")); - println!("Name: {}", person.Name()); -} \ No newline at end of file