trait3 [Rust] 고급 Trait 예제 (46일차) impl Iterator for Counter { type Item = u32; fn next(&mut self) -> Option; } pub trait Iterator { fn next(&mut self) -> Option; } use std::ops::Add; #[derive(Debug, PartialEq)] struct Point { x: i32, y: i32, } impl Add for Point { type Output = Point; fn add(self, other: Point) -> Point { Point { x: self.x + other.x, y: self.y + other.y, } } } fn main() { assert_eq!(Point { x: 1, y: 0 } + Point .. 2023. 1. 29. [Rust] Trait 예제 (44일차) #![allow(dead_code)] fn main() { trait Animal { fn eat(&self); } struct Herbivore; struct Carnivore; impl Animal for Herbivore { fn eat(&self) { println!("I eat plants"); } } impl Animal for Carnivore { fn eat(&self) { println!("I eat flesh"); } } let h = Herbivore; h.eat(); } [실행 결과] $ I eat plants fn main() { trait Animal { fn eat(&self); } struct Herbivore; struct Carnivore; impl Animal for H.. 2023. 1. 27. [Rust] Trait 예제 (24일차) use std::cmp::PartialOrd; fn largest(list: &[T]) -> T { let mut largest = list[0]; for &item in list.iter() { if item > largest { largest = item; } } largest } fn main() { let numbers = vec![34, 50, 25, 100, 65]; let result = largest(&numbers); println!("The largest number is {}", result); let chars = vec!['y', 'm', 'a', 'q']; let result = largest(&chars); println!("The largest char is {}", resu.. 2023. 1. 6. 이전 1 다음