Created
June 15, 2018 22:06
-
-
Save tonis2/d67c91bd69e389dccc9556266910d199 to your computer and use it in GitHub Desktop.
Rust where clause example
This file contains hidden or 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
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