Advertisement
NLinker

Return closure via impl Fn

May 26th, 2018
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.58 KB | None | 0 0
  1. // https://play.rust-lang.org/?gist=d5d36e5991690804f5ee539ff1fafc4d&version=stable&mode=debug
  2. #![allow(unused)]
  3. fn main() {
  4.     let line = "abc";
  5.     let f = get_add_function(">>>>");
  6.  
  7.     println!("{}", f(line));
  8. }
  9.  
  10. pub fn get_add_function<'a>(indent: &'a str) -> impl (Fn(&str) -> String) + 'a {
  11.    move |line: &str| {
  12.        if indent.is_empty() {
  13.            line.to_string()
  14.        } else {
  15.            let mut s = String::with_capacity(line.len() + indent.len());
  16.            s.push_str(indent);
  17.            s.push_str(line);
  18.            s
  19.        }
  20.    }
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement