In just a few steps, we'll explore immutable states:
- Run cargo new immutable-states to create a new application project and open the directory in Visual Studio Code.
- 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) {}
- 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); });}
- To see ...