Skip to content

Instantly share code, notes, and snippets.

@tonis2
Created June 15, 2018 22:06
Show Gist options
  • Save tonis2/d67c91bd69e389dccc9556266910d199 to your computer and use it in GitHub Desktop.
Save tonis2/d67c91bd69e389dccc9556266910d199 to your computer and use it in GitHub Desktop.
Rust where clause example
struct Moose {
name: String
}
struct Deer {
name: String
}
trait Actions {
fn baby_name(&self) -> String;
}
impl Actions for Moose {
fn baby_name(&self) -> String {
let new_name = format!("{}_{}", &self.name.to_string(), "kaka");
new_name
}
}
fn give_birth<T> (data:T) -> String where T: Actions {
let animal_baby = data.baby_name();
animal_baby
}
fn main() {
let animal = Moose {name: "tonis".to_string()};
let animal2 = Deer {name: "Steve".to_string()};
let animal_baby = give_birth(animal);
let animal_baby2 = give_birth(animal2);
println!("{}", animal_baby);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment