728x90 클론코딩94 [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. [Rust lang] 76. interior mutability and cell use std::cell::Cell; #[derive(Debug)] struct PhoneModel { company_name: String, model_name: String, screen_size: f32, memory: usize, date_issued: u32, on_sale: Cell } fn main() { let super_phone_3000 = PhoneModel { company_name: "YY Electronics".to_string(), model_name: "Super Phone 3000".to_string(), screen_size: 7.5, memory: 4_000_000, date_issued: 2020, on_sale: Cell::new(true) }; println!("{.. Rust 2023. 3. 30. [Rust lang] 75. manual debug impl mod client { pub struct InternetClinet { pub client_id: u32, } } use client::InternetClinet; #[derive(Debug)] struct Customer { fn fmt(&self, f: &mut fmt::Formatter Rust 2023. 3. 30. [Rust lang] 74. lifetimes intro /* fn returns_reference() -> &'static str { //let my_string = "David".to_string(); "David" } */ struct Book { name: &'a str, second_name: &'a str } use std::fmt::Display; fn print_thing(input: T) { } fn main() { let my_book_title = "my_book_title".to_string(); // let my_book = Book { // name: "my book" // } } */ struct Adventurer Rust 2023. 3. 30. [Rust lang] 73. inspect string and &str /* fn main() { let new_vec = [8, 9, 10]; let double_vec = new_vec .iter() .inspect(|first_item| { dbg!(first_item) }) .map(|x| x *2) .inspect(|next_item| { dbg!(next_item) }) .filter(|num| *num > 17) .collect::(); dbg!(double_vec); } */ struct Book { title: &str } fn main() { let myt_book = Book { title: "my_title" } } Rust 2023. 3. 30. [Rust lang] 72. dbg! macro /* fn main() { let my_number = 9; dbg!(my_number) println!("{my_number}"); dbg!(10); println!("Something else"); } */ fn main() { let mut my_number = dbg!(9); dbg!(my_number += 10); let new_vec = dbg!(vec![8, 9, 10]); let double_vec = dbg!(new_vec .iter() .map(|x| x * 2) .collect::()); } Rust 2023. 3. 30. [Rust lang] 71. match indices peekable /* fn user_input() -> String { "me@me.com".to_string() } fn main() { let rules = "Rule number 1: No fighting. Rule number 2: Go to bed at 8 pm. Rule number 3: Wake up at 6 am."; let rule_locations = rules.match_indices("Rule").collect::(); println!("Rule locations: {rule_locations:?}"); } */ fn main() { let just_numbers = vec![1, 5, 100]; let mut number_iter = just_numbers.iter().peekable(); for.. Rust 2023. 3. 30. 이전 1 2 3 4 5 ··· 8 다음 728x90