Created
May 13, 2018 15:03
-
-
Save balajisivaraman/5b8885503be27ded5393678be5fb2a5d to your computer and use it in GitHub Desktop.
Rust 1.25 Sample
This file contains 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
trait Animal { | |
fn walk(&self); | |
} | |
struct Dog { | |
name: String | |
} | |
impl Animal for Dog { | |
fn walk(&self) { | |
println!("dog walks: {}", self.name); | |
} | |
} | |
struct Cat { | |
name: String | |
} | |
impl Animal for Cat { | |
fn walk(&self) { | |
println!("cat walks: {}", self.name); | |
} | |
} | |
fn test<A: 'static + Animal>(animal: A) -> Box<Animal> { | |
Box::new(animal) | |
} | |
fn main() { | |
let animal = Dog { name: "Snowy".to_string() }; | |
let boxed = test(animal); | |
(*boxed).walk(); | |
} | |
fn test_num<'a>(a: &'a i32, b: &'a i32) -> &'a i32 { | |
a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment