NLinker icon

Return closure via impl Fn

NLinker | PRO | 05/26/18 12:07:22 PM UTC | 0 ⭐ | 1036 👁️ | Never ⏰ | []
Rust |

580 B

|

None

|

0 👍

/

0 👎

// https://play.rust-lang.org/?gist=d5d36e5991690804f5ee539ff1fafc4d&version=stable&mode=debug
#![allow(unused)]
fn main() {
    let line = "abc";
    let f = get_add_function(">>>>");
 
    println!("{}", f(line));
}
 
pub fn get_add_function<'a>(indent: &'a str) -> impl (Fn(&str) -> String) + 'a {
    move |line: &str| {
        if indent.is_empty() {
            line.to_string()
        } else {
            let mut s = String::with_capacity(line.len() + indent.len());
            s.push_str(indent);
            s.push_str(line);
            s
        }
    }
}

Comments