Goroutines in Rust

A goroutine is a lightweight thread of execution.

use std::{thread, time::Duration};

fn f(from: &str) {
    for i in 0..3 {
        println!("{} : {}", from, i);
    }
}

fn main() {
    // Suppose we have a function call `f(s)`. Here’s how
    // we’d call that in the usual way, running it synchronously.
    f("direct");

    // To invoke this function in a thread, use Rust's `thread::spawn`.
    // This new thread will execute concurrently with the calling one.
    let handle = thread::spawn(|| {
        f("thread");
    });

    // You can also start a thread for an anonymous function call.
    let anon_handle = thread::spawn(|| {
        println!("going");
    });

    // Wait for the spawned threads to finish 
    // (in practice, consider using a more robust approach like channels or thread synchronization).
    handle.join().unwrap();
    anon_handle.join().unwrap();

    println!("done");
}

Suppose we have a function call f(s). Here’s how we’d call that in the usual way, running it synchronously.

f("direct");

To invoke this function in a thread, use Rust’s thread::spawn. This new thread will execute concurrently with the calling one.

let handle = thread::spawn(|| {
    f("thread");
});

You can also start a thread for an anonymous function call.

let anon_handle = thread::spawn(|| {
    println!("going");
});

Our two function calls are running asynchronously in separate threads now. Wait for them to finish (for a more robust approach, use Rust’s channels, or other concurrency primitives).

handle.join().unwrap();
anon_handle.join().unwrap();

println!("done");

When we run this program, we see the output of the blocking call first, then the output of the two threads. The threads’ output may be interleaved, because threads are being run concurrently by the Rust runtime.

$ cargo run
direct : 0
direct : 1
direct : 2
thread : 0
going
thread : 1
thread : 2
done

Next we’ll look at a complement to threads in concurrent Rust programs: channels.