Let's create a simple application that visualizes incoming values on the command line:
- Run cargo new channels to create a new application project and open the directory in Visual Studio Code.
- First, let's get the basics out of the way. Open src/main.rs and add the imports and an enum structure to the file:
use std::sync::mpsc::{Sender, Receiver};use std::sync::mpsc;use std::thread;use rand::prelude::*;use std::time::Duration;enum ChartValue { Star(usize), Pipe(usize)}
- Then, inside the main function, we create a channel with the mpsc::channel() function along with two threads that take care of the sending. Afterward, we are going to use two threads to send messages to the main thread with a variable delay. Here's the code: ...