Created
May 13, 2018 15:02
-
-
Save balajisivaraman/0cfded4cab727e57e6663b3cc8da14f8 to your computer and use it in GitHub Desktop.
Rust 1.26 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 { | |
pub name: String | |
} | |
impl Animal for Dog { | |
fn walk(&self) { | |
println!("dog walks: {}", self.name); | |
} | |
} | |
struct Cat { | |
pub name: String | |
} | |
impl Animal for Cat { | |
fn walk(&self) { | |
println!("cat walks: {}", self.name); | |
} | |
} | |
fn test(cat: Cat) -> impl Animal { | |
if cat.name == "Fussy" { | |
cat | |
} else { | |
Cat { name: "Scooby".to_string() } | |
} | |
} | |
fn main() { | |
// let dog = Dog { name: "Snowy".to_string() }; | |
// let testedDog = test(dog); | |
// testedDog.walk(); | |
let cat = Cat { name: "Fussy".to_string() }; | |
let testedCat = test(cat); | |
testedCat.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