728x90 전체 글493 [Rust lang] 88. three types of generics use std::fmt::Display; fn print(input: T) { println!("Hi, I'm a {input}"); } fn print_2(input: impl Display) { println!("Hi, I'm a {input}"); } fn print_3(input: Box) { println!("Hi, I'm a {input}"); } fn main() { print_2(8); print_3(Box::new(String::from("I am a String"))); } Rust 2023. 3. 30. [Rust lang] 87. trait objects, box /* enum List { Content(i32, Box), NoContent } fn main() { let my_list = List::Content(8786, Box::new(List::NoContent)) } */ trait Booky {} struct Book { name: String } struct BigBook; struct City { year_founded: i32 } impl Booky for Book {} impl Booky for BigBook {} impl Booky for City {} fn main() { let my_city = City { year_founded: 1989 }; let vec_of_booky_things: Vec = vec![ Box::new(Book { .. Rust 2023. 3. 30. [Rust lang] 86. box struct SomeStruct { name: String, number: u8, data: Box } fn main() { let my_struct = SomeStruct { name: "Hi there".to_string(), number: 0, data: Box::new([9; 1000]) }; let my_box = Box::new(9); println!("{my_box}"); println!("{}", *my_box); println!("The struct is {} bytes", std::mem::size_of_val(&my_struct)); } Rust 2023. 3. 30. [Rust lang] 85. clippy fn print_vec_ref(input: &[i32]) { if input.is_empty() { println!("Vec is empty!"); } else { /* for num in input { println!("{num}"); } */ inpu.iter().for_each(|num| println!("{num}")); } } fn main() { /* let my_vec = vec![8, 9, 10]; print_vec_ref(&my_vec); */ /* let mut done = false; let mut counter = 0; while !done { counter += 1; if counter > 10 { done = true; } } let some_variable = Some(9); .. Rust 2023. 3. 30. [Rust lang] 84. mutex use std::sync::Mutex; use std::thread; trait CoolTrait { fn cool_function(&self); } struct OurStruct { data: Mutex } impl CoolTrait for OurStruct { fn cool_function(&self) { *self.data.borrow_mut() += 1; } } fn main() { let our_struct = OurStruct { data: Mutex::new(0) } let mut join_vec = vec![]; for _ in 0..10 { thread::spawn(|| { *our_struct.borrow_mut() += 1; }); join_vec.push(join_handle); }.. Rust 2023. 3. 30. [Rust lang] 83. multiple threads use std::thread; fn main() { /* let join_handle = thread::spawn(|| { println!("I am printing something"); }); join_handle.join(); */ let mut join_vec = vec![]; for _ in 0..10 { let handle = thread::spawn(|| { println!("I am printing something"); }); join_vec.push(handle); } join_vec.into_iter().for_each(|handle| handle.join().unwrap()); } Rust 2023. 3. 30. [Rust lang] 82. cow use std::borrow::Cow; fn modulo_3(input: u8) -> Cow { name: Cow { name: Cow Rust 2023. 3. 30. [Rust lang] 81. type alias and todo macro struct SomeType { name: String, number: u8 } fn some_function() -> Vec { todo!(); // unimplemented!(); == todo!() } fn main() { some_function(); } Rust 2023. 3. 30. [Rust lang] 80. rust 1.59 use std::thread::available_parallelism; fn main() { let (mut x, mut y) = (9, 10); (x, y) = (10, 11); let a = available_parallelism().unwrap(); println!("We have a {a} cores") let my_num = "99".parse::().map(|num| &num); println!("{my_num:?}"); } Rust 2023. 3. 30. [Rust lang] 79. rc use std::rc::Rc; fn takes_a_string(input: Rc) { } println!("{input}"); fn also_takes_a_string(input: Rc) { println!("{input}"); } fn main() { let my_string = "Hello there".to_string(); takes_a_string(Rc::clone(&my_string)); also_takes_a_string(Rc::clone(&my_string)); } use std::rc::Rc; #[derive(Debug)] struct City { name: String, population: u32, history: Rc } #[derive(Debug)] struct CityData { .. Rust 2023. 3. 30. [Rust lang] 78. why interior mutability use std::cell::{Cell, RefCell}; #[derive(Debug)] trait SuperCoolTrait { fn cool_function(&self); } struct User { id: u32, times_used: u32 } impl SuperCoolTrait for User { fn cool_function(&self) { let times_used = self.times_used.get(); self. times_used.set(times_used + 1); } } fn main() { let user = User { id: 89723987. times_used: Cell::new(0) } for _ in 0..20 { user.cool_function(); } println.. Rust 2023. 3. 30. [Rust lang] 77. refCell /* use std::cell::Cell; fn main() { let my_cell = Cell::new(String::from("I am a String")); my_cell.set(String::from("I am a String??!?!?!?")); let my_string = my_cell.get(); } */ use std::cell::Cell; #[derive(Debug)] struct User { id: u32, year_registered: u32, username: String, active: RefCell } fn main() { let user_1 = User { id: 1, year_registered: 2020, username: "User 1".to_string(), activ.. Rust 2023. 3. 30. 이전 1 2 3 4 5 ··· 42 다음 728x90