book
book copied to clipboard
ch19-04: explanation doesn't match error under "Returning Closures"
- [x] I have checked the latest
mainbranch to see if this has already been fixed - [x] I have searched existing issues and pull requests for duplicates
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 explanation after the compiler error doesn't match its contents. The error doesn't reference the Sized trait and it would also be nice to have an explanation of why we're not exactly following the compiler's advice.
Suggested fix:
Remove the mention of the Sized trait and explain why we choose to use Box<dyn Fn(i32) -> i32> instead of impl Fn(i32) -> i32.
Thanks for the issue. This is interesting—I am not sure if the compiler message changed at some point, but it still implicitly references Sized:
1 | fn returns_closure() -> dyn Fn(i32) -> i32 {
| ^^^^^^^^^^^^^^^^^^ doesn't have a size known at compile-time
I do agree that it might be nice to say something about the fact that we’re using Box<dyn Fn(i32) -> i32> if we keep it that way; but the better move here probably is to do what the compiler says (and then maybe note that we could also return a trait object, but elide the details). I’m flagging this one up for fixing as part of the 2024 edition revisions. See also #1514, which this is directly related to!