Skip to content

2018 Edition: Chapter 19: Returning Closures suggest Box and not impl trait #1514

@jeanphilippeD

Description

@jeanphilippeD

Hello,
I searched the issues but did not find an existing relevant one.

The only provided solution for returning closure in this section is using Box:
https://doc.rust-lang.org/book/2018-edition/ch19-05-advanced-functions-and-closures.html#returning-closures

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

This now compiles on Rust stable, so it seem like it would be a good alternative to put for this advance topic section:
https://play.rust-lang.org/?gist=ce094b09ea045b0e40a69d3fc3158500&version=stable&mode=debug&edition=2015

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

fn main() {
    let c = returns_closure();
    println!("{}", c(5));
}

Thanks,
Jean-Philippe.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions