Created
October 28, 2021 14:37
-
-
Save BruJu/63732f266e73b8a54ede441787f32193 to your computer and use it in GitHub Desktop.
two_legs.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The feature that Rust needs | |
trait Animal { const LEGS: u32; } | |
fn walk_on_your_two_legs<A>(_animal: &A) | |
where A: Animal | |
// , A::LEGS == 2 :( | |
{ | |
println!("I'm walking on my two legs") | |
} | |
struct Human { } | |
impl Animal for Human { const LEGS: u32 = 2; } | |
struct Dog { } | |
impl Animal for Dog { const LEGS: u32 = 4; } | |
fn main() { | |
let tintin = Human { }; | |
let snowy = Dog { }; | |
walk_on_your_two_legs(&tintin); | |
walk_on_your_two_legs(&snowy); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment