-
Rust☆연습(Traits)_part1코딩Coding/Rust연습 2022. 9. 11. 11:21728x90
// Dungeons and Dragons struct Dwarf { name: String, } struct Elf { name: String, } struct HalfOrc { name: String, } struct Human { name: String, } impl Constitution for Elf {} impl Constitution for Human {} // The constitution bonus for a dwarf is 2 impl Constitution for Dwarf { fn constitution_bonus(&self) -> u8 { 2 } } impl Constitution for HalfOrc { fn constitution_bonus(&self) -> u8 { 1 } } pub trait Constitution { fn constitution_bonus(&self) -> u8 { 0 } } fn main() { let my_dwarf = Dwarf { name: String::from("NellDwarf"), }; let my_elf = Elf { name: String::from("NellElf"), }; let my_half_orc = HalfOrc { name: String::from("NellHalfOrc"), }; let my_human = Human { name: String::from("NellHuman"), }; // Return 2 my_dwarf.constitution_bonus(); // Return 1 dbg!(my_human.constitution_bonus()); // Return 0 dbg!(my_elf.constitution_bonus()); my_human.constitution_bonus(); }
반응형'코딩Coding > Rust연습' 카테고리의 다른 글
Rust연습)벡터안에 0 을 10개 넣기 (0) 2023.06.08 Rust WebSocket구현 ChatGPT (0) 2023.05.28 rust연습❤️let mut p: f64 = 16us.pow(2) as f64; (0) 2022.09.12 Rust☆연습_Traits_part2 (0) 2022.09.11 러스트연습숫자가 0부터 계속 올라감 계속 그 자리에서 출력 (0) 2022.08.14 Rust연습_H가 같은줄에 출력에서 앞으로 계속 쌓임. (0) 2022.08.14 Rust연습❤️물어보고 출력 무한 loop (0) 2022.08.14 Rust Machine learning code 불완전함 (0) 2022.08.13