Trait Based Polymorphism | Rust Language

Опубликовано: 30 Май 2024
на канале: Learning Rust
223
8

Learning to use Traits with enums and multiple structs | Polymorphism.

"This Rust code defines a trait Animal with a method make_sound(). Two structs, Cat and Dog, implement this trait. An enum Pet is defined to represent either a cat or a dog. The Animal trait is then implemented for the Pet enum, delegating the make_sound() method based on the variant.

A function sounds() takes a string representing a pet type ("cat" or "dog") and returns a corresponding Pet. In the main() function, it creates a pet based on the input string and prints its sound."

Links:
🟩 https://tourofrust.com/index.html
🟩 https://github.com/RGGH/return_trait
🟩 https://webdock.io/en?maff=wdaff--170
🟩 https://users.rust-lang.org/t/polymor...

Code:
🟩 https://github.com/RGGH/trait_impl_enums

#Traits #rustprogramming #learningrust


На этой странице сайта вы можете посмотреть видео онлайн Trait Based Polymorphism | Rust Language длительностью часов минут секунд в хорошем качестве, которое загрузил пользователь Learning Rust 30 Май 2024, поделитесь ссылкой с друзьями и знакомыми, на youtube это видео уже посмотрели 223 раз и оно понравилось 8 зрителям. Приятного просмотра!