How to do it...

In just a few steps, we'll explore immutable states:

  1. Run cargo new immutable-states to create a new application project and open the directory in Visual Studio Code.
  2. First, we'll add the imports and a noop function to call to our src/main.rs file:
use std::thread;use std::rc::Rc;use std::sync::Arc;use std::sync::mpsc::channel;fn noop<T>(_: T) {}
  1. Let's explore how different types can be shared across threads. The mpsc::channel type provides a great out-of-the-box example of a shared state. Let's start off with a baseline that works as expected:
fn main() {    let (sender, receiver) = channel::<usize>();    thread::spawn(move || {        let thread_local_read_only_clone = sender.clone();        noop(thread_local_read_only_clone);    });}
  1. To see ...

Get Rust Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.