-
Rust☆연습_Traits_part2코딩Coding/Rust연습 2022. 9. 11. 14:00728x90
// Dungeons and Dragons struct Dwarf { name: String, } struct Elf { name: String, } struct HalfElf { 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 } } // Let's make a function for speaking Elvish pub trait Elvish {} impl Elvish for Elf {} impl Elvish for HalfElf {} impl Elvish for HalfOrc {} pub fn speak_elvish<T: Elvish>(character: T) -> String { String::from("yes") } pub fn no_speak_elvish<T: Elvish>(character: T) -> String { String::from("no") } 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"), }; let my_half_elf = HalfElf { name: String::from("NellElf"), }; // Return 2 my_dwarf.constitution_bonus(); // Return 1 dbg!(my_half_orc.constitution_bonus()); // Return 0 dbg!(my_elf.constitution_bonus()); dbg!(my_human.constitution_bonus()); // Return "yes" dbg!(speak_elvish(my_elf)); // Return "yes" dbg!(speak_elvish(my_half_elf)); // Return "yes" dbg!(no_speak_elvish(my_half_orc)); }
결과
Compiling playground v0.0.1 (/playground) warning: unused variable: `character` --> src/main.rs:52:32 | 52 | pub fn speak_elvish<T: Elvish>(character: T) -> String { | ^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_character` | = note: `#[warn(unused_variables)]` on by default warning: unused variable: `character` --> src/main.rs:56:35 | 56 | pub fn no_speak_elvish<T: Elvish>(character: T) -> String { | ^^^^^^^^^ help: if this is intentional, prefix it with an underscore: `_character` warning: field `name` is never read --> src/main.rs:3:5 | 2 | struct Dwarf { | ----- field in this struct 3 | name: String, | ^^^^^^^^^^^^ | = note: `#[warn(dead_code)]` on by default warning: field `name` is never read --> src/main.rs:7:5 | 6 | struct Elf { | --- field in this struct 7 | name: String, | ^^^^^^^^^^^^ warning: field `name` is never read --> src/main.rs:11:5 | 10 | struct HalfElf { | ------- field in this struct 11 | name: String, | ^^^^^^^^^^^^ warning: field `name` is never read --> src/main.rs:15:5 | 14 | struct HalfOrc { | ------- field in this struct 15 | name: String, | ^^^^^^^^^^^^ warning: field `name` is never read --> src/main.rs:19:5 | 18 | struct Human { | ----- field in this struct 19 | name: String, | ^^^^^^^^^^^^ warning: `playground` (bin "playground") generated 7 warnings Finished dev [unoptimized + debuginfo] target(s) in 0.61s Running `target/debug/playground` [src/main.rs:85] my_half_orc.constitution_bonus() = 1 [src/main.rs:88] my_elf.constitution_bonus() = 0 [src/main.rs:89] my_human.constitution_bonus() = 0 [src/main.rs:92] speak_elvish(my_elf) = "yes" [src/main.rs:95] speak_elvish(my_half_elf) = "yes" [src/main.rs:98] no_speak_elvish(my_half_orc) = "no"
한글러스트Rust강의_017_traits_기초 #rustlang #rust #traits
반응형'코딩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)_part1 (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