Created
October 12, 2017 20:09
-
-
Save anonymous/12c10b70941018be7e1dc084e92154ba to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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
fn main() { | |
let a = Test(None); | |
a.print(); | |
let b = Test(Some(23));//Box::new(Test(Some(23))); | |
b.print(); | |
print_it(&b); | |
} | |
struct Test (Option<i32>); | |
impl Print for Test { | |
fn print_value(&self) -> Option<i32> { | |
self.0 | |
} | |
} | |
trait Print { | |
fn print_value(&self) -> Option<i32>; | |
fn print(&self) { | |
if let Some(ref v) = self.print_value() { | |
if *v > 100 { | |
println!("{}", v); | |
} | |
} | |
} | |
} | |
fn print_it<T: Print>(s: &T) { | |
let v = s.print_value().unwrap_or_else(|| -1); | |
if v > 100 { | |
println!("{}", v); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment