Rust programming video tutorial (Advanced) — 028_ 2 return closure

Time:2021-7-22

Video address

Headline address:https://www.ixigua.com/i677586170644791348…
Station B address:https://www.bilibili.com/video/av81202308/

Source address

GitHub address:https://github.com/anonymousGiga/learn_rus…

Explanation content

Return closure
Error example:

fn returns_closure() -> Fn(i32) -> i32 { |x| x + 1 }

Correct example:

fn returns_closure() -> Box<dyn Fn(i32) -> i32> {
    Box::new(|x| x + 1)
}

fn main() {
    let c = returns_closure();
    println!("r = {}", c(1));
    //Equivalent to println(“ r = {}", (*c)(1)); // Dereference polymorphism
    println!("Hello, world!");
}

This work adoptsCC agreementReprint must indicate the author and the link of this article

Linghu Yichong