ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Rust☆연습_Traits_part2
    코딩Coding/Rust연습 2022. 9. 11. 14:00
    728x90
    // 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

     

    https://youtu.be/YNPFIVGBCjE

     

     

     

     

     

     

     

     

     

     

     

     


     

    반응형
Designed by Tistory.