The async mailbox

In this exercise, you will learn how to turn a small server implemented using threads into an asynchronous server.

Copy /solutions/threaded-mailbox to your work directory.

1. Add async-std or tokio as a dependency

Open Cargo.toml in your server and add:

async-std = "1.6"


# ...
tokio = { version = "1", features = ["full"] }

2. Replace sync types by async types

Replace all synchronous/blocking types (mostly ThreadTask, Mutex, TcpListener) by async types.

Turn functions into async functions if necessary.

For async-std, the most relevant examples are:

For tokio, the most relevant examples are:

Use those types and modules to replace the types from stdlib.

3. Observe the result

What changed? What stayed the same? Describe your experience.

4. Testing

You can test the server by sending:

$ echo "PUBLISH 123231213" | nc 7878
$ echo "RETRIEVE" | nc 7878

PUBLISH <message> will insert a message into the mailbox. RETRIEVE will retrieve a message.

Alternatively, you can use the provided tcp-client example:

$ cargo run -- "PUBLISH 12345"
$ cargo run -- "RETRIEVE"

5. Additional Exercise

  • Make the program listen on IPV4 and IPV6.

  • Implement the program using smol and friends instead of the above