Skip to content

MaxLuchs/trait_test

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Exercises to: https://www.twitch.tv/videos/903845418

Just using Box<dyn trait>

Example: let people: Vec<Box<dyn trait>> = vec![josef, maria, gabriel]

Cons:

  • Getting from Box<dyn trait> back to the concrete Type is NOT easily possible anymore.

Alternative solution for using Box<dyn trait> for Traits: https://bennetthardwick.com/blog/dont-use-boxed-trait-objects-for-struct-internals/

Define an Enum containing all subtypes and implementing the Trait

Cons:

  • Enum not extensible anymore afterwards on the fly for new concrete types

Example:

enum Personality { Father(Father), Mother(Mother), Angel(Angel) }
impl Personality for People {
    fn say_hello(&self) {
        match(*self) {
            Father(father) => father.say_hello(),
            Mother(mother) => mother.say_hello(),
            Angel(angel) => angel.say_hello()
        }
    }
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages