Created
October 23, 2015 18:40
-
-
Save anonymous/ca48a102e7dda5c5a0db to your computer and use it in GitHub Desktop.
Shared via Rust 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
#[derive(Debug)] | |
struct Foo(u32); | |
fn foo(val: u32) -> Foo { Foo(val) } | |
impl Foo { | |
fn new() -> Self { Foo(3) } | |
} | |
trait BoolExt { | |
fn into_option<F, T>(self, f: F) -> Option<T> | |
where F: Fn() -> T, Self: Sized; | |
} | |
impl BoolExt for bool { | |
fn into_option<F, T>(self, f: F) -> Option<T> | |
where F: Fn() -> T, Self: Sized | |
{ | |
if self { Some(f()) } else { None } | |
} | |
} | |
fn main() { | |
println!("{:?}", true.into_option(Foo::new)); | |
println!("{:?}", false.into_option(|| foo(3))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment