Answer: D. X gives compilation error, Y prints "Kernel"
"The task supplied to `spawn` must implement `Send`. However, it is
possible to **use** `!Send` values from the task as long as they only
exist between calls to `.await`."
I am still trying to find reason why X gives compilation error. The Y will work since it is get dropped before the next await (line 15)
------
I have attached few more example code snippets.
In this example, both e1 and e2 will work
In 1st, there is no await inside the spawn block so it allows
In 2nd, everything gets dropped before next await (which makes you doubt why Y in the quiz didn't work) and works
Reference:
https://docs.rs/tokio/latest/tokio/task/fn.spawn.html#using-send-values-from-a-task
@AstraKernel I guessed C, and as usual I guessed wrong :) Can't really come up with any justification for Rust doing what it does here.
When running in playground, it's very hit-and-miss if Y actually ends up printing "Kernel" or not. I guess there's a race condition between finishing the spawned task and exiting the process.
I also have no idea why it behaves like that
About Y, you are right. I should have placed await better.
Attached: 1 image Answer: D. X gives compilation error, Y prints "Kernel" "The task supplied to `spawn` must implement `Send`. However, it is possible to **use** `!Send` values from the task as long as they only exist between calls to `.await`." I am still trying to find reason why X gives compilation error. The Y will work since it is get dropped before the next await (line 15) ------ I have attached few more example code snippets. In this example, both e1 and e2 will work In 1st, there is no await so it allows inside the spawn block In 2nd, everything gets dropped before next await (which makes you doubt why Y in the quiz didn't work) and works Reference: https://docs.rs/tokio/1.37.0/src/tokio/task/spawn.rs.html