Created
February 18, 2020 13:58
-
-
Save badboy/b662e1629955a7afdf26ff95ea85d8f0 to your computer and use it in GitHub Desktop.
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() { | |
bind("a", |a| { | |
println!("{}", a) | |
}); | |
bind(("a", "b"), |(a, b)| { | |
println!("{} {}", a, b) | |
}); | |
/* | |
Uncomment to show the error: | |
`bind` did take a 2-tuple, but the closure expects 3 arguments. | |
```text | |
error[E0593]: closure is expected to take a single 2-tuple as argument, but it takes 3 distinct arguments | |
--> src/main.rs:12:5 | |
| | |
12 | bind(("a", "b"), |a, b, c| { | |
| ^^^^ --------- takes 3 distinct arguments | |
| | | |
| expected closure that takes a single 2-tuple as argument | |
```` | |
*/ | |
//bind(("a", "b"), |a, b, c| { | |
// println!("{} {} {}", a, b, c) | |
//}); | |
} | |
pub trait Bind<Params> { | |
fn bind<F: 'static>(params: Self, func: F) | |
where | |
Self: Sized, | |
F: FnMut(Params); | |
} | |
pub fn bind<Binder, Params, F>(params: Binder, func: F) | |
where | |
Binder: Bind<Params>, | |
F: 'static, | |
F: FnMut(Params) | |
{ | |
Binder::bind(params, func) | |
} | |
impl Bind<String> for &str { | |
fn bind<F: 'static>(params: Self, mut func: F) | |
where | |
Self: Sized, | |
F: FnMut(String) | |
{ | |
let param = params.to_uppercase(); | |
func(param); | |
} | |
} | |
impl Bind<(String, String)> for (&str, &str) { | |
fn bind<F: 'static>(params: Self, mut func: F) | |
where | |
Self: Sized, | |
F: FnMut((String, String)) | |
{ | |
let params = (params.0.to_uppercase(), params.1.to_uppercase()); | |
func(params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment