#RustQuiz : What is the output?

A. Compilation error in X & Y

B. Runtime error in X & Y

C. X prints "boo" and "kernel", Y Prints "Kernel"

D. X gives compilation error, Y prints "Kernel"

vote below

#rustlang #rust

vote here
A. Compilation error in X & Y
8.3%
B. Runtime error in X & Y
0%
C. X prints "boo" and "kernel", Y Prints "Kernel"
83.3%
D. X gives compilation error, Y prints "Kernel"
8.3%
Poll ended at .

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

spawn in tokio::task - Rust

Spawns a new asynchronous task, returning a `JoinHandle` for it.

@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.

@Corax42

I also have no idea why it behaves like that

About Y, you are right. I should have placed await better.

https://infosec.exchange/@AstraKernel/112414620569428550

Astra Kernel :verified: (@[email protected])

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

Infosec Exchange