Skip to content

Instantly share code, notes, and snippets.

@lkuper
Last active December 21, 2015 16:58
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 lkuper/6336728 to your computer and use it in GitHub Desktop.
Save lkuper/6336728 to your computer and use it in GitHub Desktop.
enum Color { cyan, magenta, yellow, black }
impl Eq for Color {
fn eq(&self, other: &Color) -> bool {
match (*self, *other) {
(cyan, cyan) => { true }
(magenta, magenta) => { true }
(yellow, yellow) => { true }
(black, black) => { true }
_ => { false }
}
}
// Rust 0.7 needs this.
fn ne(&self, other: &Color) -> bool { !self.eq(other) }
}
// A bounded quantification example -- this is where traits are handy.
fn member<T: Eq>(elem: T, vec: ~[T]) -> bool {
// 0.7 syntax:
for vec.iter().advance |vec_elem| {
// 0.8 syntax:
// for vec_elem in vec.iter() {
if elem == *vec_elem { return true; }
}
return false;
}
fn main() {
println(fmt!("%?", member(magenta, ~[black, yellow, magenta, black])));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment