I'm a new Rustacean, thank you all for writing such a detailed tutorial.
Now I'm confusing about the code in Listing 20-21:
trait FnBox {
fn call_box(self: Box<dyn Self>);
}
impl<F: FnOnce()> FnBox for F {
fn call_box(self: Box<dyn F>) {
(*self)()
}
}
type Job = Box<dyn FnBox + Send + 'static>;
From 1.27, Box<Trait> becomes Box<dyn Trait>, I've got compile errors:
error[E0411]: expected trait, found self type `Self`
--> src/lib.rs:10:31
|
10 | fn call_box(self: Box<dyn Self>);
| ^^^^ `Self` is only available in traits and impls
error[E0404]: expected trait, found type parameter `F`
--> src/lib.rs:14:31
|
14 | fn call_box(self: Box<dyn F>) {
| ^ did you mean `Fn`?
After removing all 3 dyns, everything goes well, without errors and successfully running.
Rust version I'm using:
rustc 1.27.2 (58cc626de 2018-07-18)
cargo 1.27.0 (1e95190e5 2018-05-27)
Chapter Link: https://doc.rust-lang.org/nightly/book/2018-edition/ch20-02-multithreaded.html
You're right that I messed this up! Can you try this?
trait FnBox {
fn call_box(self: Box<Self>);
}
impl<F: FnOnce()> FnBox for F {
fn call_box(self: Box<F>) {
(*self)()
}
}
type Job = Box<dyn FnBox + Send + 'static>;
@steveklabnik Yes it works.
And I find that it's also how full code example, which is referenced in chapter 20-03, wrote.
Ah great; sounds like I (or someone else!) needs to just change that one part then. Thank you!
Hi @steveklabnik, that solution works great, but it looks like it's only been changed on the second edition of the book. I'm following the 2018 edition, and it still contains the non working code that uses Box<dyn Self>:
https://doc.rust-lang.org/book/2018-edition/ch20-02-multithreaded.html
@srodrigo I think the change has not yet made its way to stable, see https://doc.rust-lang.org/beta/book/2018-edition/ch20-02-multithreaded.html
that said it seems like some of the text still refers to it so there's more work to do.
Looks like the text around this example was fixed in 3d47ef5fb1c4155b5ac005007030129e6b058478 so I'm closing!
Most helpful comment
You're right that I messed this up! Can you try this?