Skip to content

Instantly share code, notes, and snippets.

Created January 24, 2017 17:33
Show Gist options
  • Save anonymous/4d72a82355025ce42e7bd3c13c9e5059 to your computer and use it in GitHub Desktop.
Save anonymous/4d72a82355025ce42e7bd3c13c9e5059 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
trait Entity {
fn walk(&self) -> String;
}
struct Dog {
name: String,
age: i32
}
impl Dog {
fn new(name: &str, age: i32) -> Dog {
Dog{name: name.to_owned(), age: age}
}
}
impl Entity for Dog {
fn walk(&self) -> String {
format!("{} who is {} years old, is walking.", self.name, self.age)
}
}
impl Entity for Box<Entity> {
fn walk(&self) -> String {
self.walk()
}
}
fn generate_entity() -> Box<Entity> {
Box::new(Dog::new("marcel", 2))
}
fn process_entity<E: Entity>(ent: &E) -> &E {
ent.walk();
ent
}
fn main() {
let p = generate_entity();
let e = process_entity(&p);
let f = e.clone();
println!("{}", e.walk());
println!("{}", p.walk());
println!("{}", f.walk());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment