Skip to content

Instantly share code, notes, and snippets.

@BruJu
Created October 28, 2021 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BruJu/63732f266e73b8a54ede441787f32193 to your computer and use it in GitHub Desktop.
Save BruJu/63732f266e73b8a54ede441787f32193 to your computer and use it in GitHub Desktop.
two_legs.rs
// 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