Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
K0n4ta13 opened this issue Feb 3, 2025 · 1 comment
Open

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

K0n4ta13 opened this issue Feb 3, 2025 · 1 comment

Comments

@K0n4ta13
Copy link

K0n4ta13 commented Feb 3, 2025

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.

@chriskrycho
Copy link
Contributor

Thanks for flagging this up! Funky! I’ll probably tackle it by making the item actually be a closure (possibly and also adding a clarifying note). If we put on our pedantry hats, after all, the example in the code… isn’t a closure in a strict sense! It does not close over anything!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants