Hacker News new | past | comments | ask | show | jobs | submit login

This is confusing, and Rust's error messages make it much worse. If you try to remove the `move`, you get an error:

> closure may outlive the current function, but it borrows `answer`, which is owned by the current function. To force the closure to take ownership of `answer` use the `move` keyword.

But 'answer' is of course not owned by the current function, and how can you take ownership through a shared reference?

The explanation is double indirection. By default, the closure captures a pointer to answer, which is itself a pointer on the stack. Without the 'move', inside the closure `answer` has type &&str and points into make_tester's stack frame. With the 'move', it copies the passed-in pointer. The error message is referring to the pointer itself, and this is not obvious.

Incidentally I have never found docs for the '+' syntax there, would appreciate a pointer to any.




> But 'answer' is of course not owned by the current function

As you imply later, ‘answer’ itself is owned by the current function, but the ‘answer’ value is a pointer that doesn’t own what it points to. This subtlety, that references are full values themselves, is definitely something to trip on, especially when double indirection starts popping up like this.


The + syntax is https://doc.rust-lang.org/reference/types/trait-object.html#...

This applies to impl Trait as well. It should probably be there too...


Thanks!




Consider applying for YC's W25 batch! Applications are open till Nov 12.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: