728x90 Rust95 [Rust lang] 96. test-driven development fn math(input: &str) -> i32 { 9 } #[cfg(test)] mod tests { use super::math; #[test] fn one_plus_one_is_two() { assert_eq!(math("1 + 1"), 2); } #[test] fn one_minus_two_is_minus_one() { assert_eq!(math("1 - 2"), -1); } #[test] fn one_minus_minus_one_is_two() { assert_eq!(math("1 - -1"), 2); } } fn main() { } const OKAY_CHARACTERS: &str = "1234567890+- "; fn math(input: &str) -> i32 { if input.cha.. Rust 2023. 3. 31. [Rust lang] 95. builder pattern #[derive(Debug)] struct Character { name: String, age: u8, height: u32, weight: u32, lifestate: LifeState } #[derive(Debug)] enum LifeState { Alive, Dead, NeverAlive, Uncertain } impl Default for Character { fn default() -> Self { Self { name: "Billy".to_string(), age: 15, height: 170, weight: 70, lifestate: LifeState::Alive } } } impl Character { fn with_age(mut self, age: u8) -> Self { self.ag.. Rust 2023. 3. 30. [Rust lang] 94. add trait use std::ops:Add; #[derive(Debug)] struct Country { name: String, population: u32, gdp: u32 } impl Country { fn new(name: &str, population: u32, gdp: u32) -> Self { Self { name: name.to_string(), population, gdp } } } impl Add for Point { type Output = Self; fn add(self, other: Self) -> Self { Self { x: self.x + other.x, y: self.y + other.y, name: format!("{} and {}", self.name, other.name), pop.. Rust 2023. 3. 30. [Rust lang] 93. deref trait use std::ops::{Deref, DerefMut}; struct HoldsANumber(u8); impl Deref for HoldsANumber { type Target = u8; fn deref(&self) -> &Self::Target { &self.0 } } impl DerefMut for HoldsANumber { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.0 } } fn main() { let my_number = HoldsANumber(20); println!("{}", my_number.checked_add(10).unwrap() + 20); } use std::ops::Deref; struct Character { name.. Rust 2023. 3. 30. [Rust lang] 92. attribute part #[cfg(target_os = "linux")] fn do_something_if_linux() { println!("I am running in Linux"); } #[cfg(target_os = "windows")] fn do_something() { println!("I am running in Windows"); } struct JustAStruct {} fn main() { let some_char = 'a'; do_something(); } #[test] fn tests_a_thing() { } #[test] fn tests_another_thing() { } #[deprecated] fn test() { } fn main() { } Rust 2023. 3. 30. [Rust lang] 91. about impl trait use std::fmt::Display; fn generic_function(input: T) { println!("{input}"); } type MyString = impl Display; fn impl_function(input: impl Display) { println!("{input}"); } fn returns_a_closure() -> impl Fn(i32) { |x| println!("{x}") } fn main() { let my_number = 9; let my_closure = returns_a_closure(); my_closure(my_number); generic_function(8); } Rust 2023. 3. 30. [Rust lang] 90. fn mut fnonce fn fn_closure(f: F) where F: Fn() { f(); } fn fn_mut_closure(mut f: F) where F: FnMut(), { f(); } fn fn_once_closure(f: F) where F: FnOnce(), { f(); } fn main() { let mut my_string = String::from("Hello there"); /* let print_it = || { drop(my_string); }; print_if(); */ fn_once_closure(|| { my_string.push('a); println!("{my_string}"); drop(my_string); }) } Rust 2023. 3. 30. [Rust lang] 89. function pointers fn gives_five() -> u8 { 5 } fn gives_six() -> impl FnMut { 6 } fn add_to_function_output(my_function: fn() -> u8, some_number: u8) { let my_number = my_function(); let next_number = my_number + some_number; println!("We got {next_number"); } fn main() { add_to_function_output(gives_five, 8); add_to_function_output(gives_six, 8); } Rust 2023. 3. 30. [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. 이전 1 2 3 4 ··· 8 다음 728x90