Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- fn main() {
- println!("{}", make_adder(1)(2));
- println!("{}", make_adder(2)(3));
- let indent = "xxx".to_owned();
- let f = make_str_adder(&indent);
- println!("add = {}", f("_abc_"));
- }
- fn make_adder(a: i32) -> Box<Fn(i32) -> i32> {
- if a == 1 {
- Box::new(move |b| a + b)
- } else {
- Box::new(move |b| a * b)
- }
- }
- fn make_str_adder<'a>(a: &'a str) -> Box<Fn(&str) -> String + 'a> {
- if a.chars().all(|c| c.is_ascii_whitespace()) {
- Box::new(move |l| String::from(l))
- } else {
- Box::new(move |l| {
- let mut r = String::new();
- r.push_str(a);
- r.push_str(l);
- r
- })
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement