Skip to content

ch19-05: Error with the fn type and closures #4220

@K0n4ta13

Description

@K0n4ta13

URL to the section(s) of the book with this problem:

https://doc.rust-lang.org/book/ch19-05-advanced-functions-and-closures.html#returning-closures

Description of the problem:

The book says that “you’re not allowed to use the function pointer fn as a return type”. However, the provided example successfully compiles using the fn type.

example in the main branch:

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

does compile and is allowed to use the fn type.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6389cacd2301160f942f838a99bbb82b

Suggested fix:

Clarify that a closure that does not capture its environment can be coerced into a function pointer.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions