Skip to content

Instantly share code, notes, and snippets.

@nayato
Forked from anonymous/playground.rs
Created October 12, 2017 20:09
Show Gist options
  • Save nayato/90ac61d3c4da299cc6a6cb95ac42f4fb to your computer and use it in GitHub Desktop.
Save nayato/90ac61d3c4da299cc6a6cb95ac42f4fb to your computer and use it in GitHub Desktop.
Rust code shared from the playground
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