Concurrency is an essential concept in modern programming. This challenge focuses on building a message processing system using Rust's channels and threads. You'll create a system where multiple producers send messages with different priority levels, and a single consumer processes and formats these messages.
Implement three key functions that work with a message processing system:
create_message_channel()
- Creates a channel for sending Message structscreate_producer_thread()
- Creates a thread that analyzes and forwards messagescreate_consumer_thread()
- Creates a thread that formats and collects messages[PRIORITY|SENDER_ID] CONTENT
where PRIORITY is one of: LOW, MED, HIGH, CRITunwrap()
main
function to see how the functions are used.Here are some tips to help you get started:
mpsc::channel()
to create the message channelthread::spawn
and move closureswhile let Ok(msg) = rx.recv()
for receivingformat!("[{}|{}] {}", priority, id, content)
use std::fmt;use std::sync::mpsc::{ self, channel, Receiver, Sender };use std::thread::{ self, JoinHandle };#[derive(Clone)]pub enum Priority { Low, Medium, High, Critical,}#[derive(Clone)]pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match self { Priority::Low => "LOW", Priority::Medium => "MEDIUM", Priority::High => "HIGH", Priority::Critical => "CRITICAL", }; write!(f, "{}", s) }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for mut message in messages { // Update priority based on content message.priority = match message.content { ref content if content.contains("ERROR") => Priority::Critical, ref content if content.contains("WARNING") => Priority::High, ref content if content.contains("INFO") => Priority::Medium, _ => Priority::Low, }; // Send the updated message through the channel if let Err(e) = tx.send(message) { eprintln!("Failed to send message: {}", e); break; } } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel let mut rs = Vec::new(); thread::spawn(move || { for message in rx.into_iter() { rs.push(format!("[{}|{}] {}", message.priority, message.sender_id, message.content)); } rs.clone() }) // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, } ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for message in messages { let priority = match &message.content.as_str() { s if s.contains("ERROR") => Priority::Critical, s if s.contains("WARNING") => Priority::High, s if s.contains("DEBUG") => Priority::Medium, _ => Priority::Low, }; let _ = tx.send(Message {priority, ..message}); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx.iter().map(|message| { let priority_str = match message.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; format!("[{priority_str}|{}] {}", message.sender_id, message.content) }).collect() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{ Receiver, Sender, channel};use std::thread::{self, JoinHandle};use std::fmt;pub enum Priority { Low, Medium, High, Critical,}impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", match self { Priority::Low => "LOW", Priority::Medium => "MEDIUM", Priority::High => "HIGH", Priority::Critical => "CRITICAL", }) }} pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for msg in messages { let priority = match &msg.content.as_str() { b if b.contains("ERROR") => Priority::Critical, b if b.contains("WARNING") => Priority::High, b if b.contains("Debug") => Priority::Medium, _ => Priority::Low }; tx.send(Message {priority, ..msg}).unwrap(); } }) } pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx.iter() .map(|x| return format!("[{}|{}] {}", x.priority, x.sender_id, x.content)) .collect() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt;pub enum Priority { Low, Medium, High, Critical,}impl fmt::Display for Priority{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let val = match self { Priority::Low => "LOW", Priority::Medium => "MEDIUM", Priority::High => "HIGH", Priority::Critical => "CRITICAL" }; write!(f, "{}", val ) }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}fn prio_for_content<T: AsRef<str>>( content: &T) -> Priority { if content.as_ref().contains("ERROR") { return Priority::Critical; } if content.as_ref().contains("WARNING") { return Priority::High; } if content.as_ref().contains("DEBUG") { return Priority::Medium; } return Priority::Low;}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel std::thread::spawn( move || { for mut m in messages { let prio = prio_for_content(&m.content); tx.send(Message{priority: prio, ..m}).unwrap(); } } )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages std::thread::spawn( move || { /*let mut res = vec![]; while let Ok( Message{content, sender_id, priority}) = rx.recv() { res.push(format!("[{}|{}] {}", priority, sender_id, priority)); } return res;*/ rx.iter().map(|x| return format!("[{}|{}] {}", x.priority, x.sender_id, x.content)) .collect() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt;use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Priority::Critical => write!(f, "CRITICAL"), Priority::High => write!(f, "HIGH"), Priority::Medium => write!(f, "MEDIUM"), Priority::Low => write!(f, "LOW"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for msg in messages { let priority = match &msg.content { content if content.contains("ERROR") => Priority::Critical, content if content.contains("WARNING") => Priority::High, content if content.contains("DEBUG") => Priority::Medium, _ => Priority::Low, }; tx.send(Message { priority, ..msg }).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || { rx.iter() .map(|x| return format!("[{}|{}] {}", x.priority, x.sender_id, x.content)) .collect() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{channel, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { write!( f, "{}", match self { Self::Critical => "CRIT", Self::High => "HIGH", Self::Medium => "MED", Self::Low => "LOW", } ) }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for message in messages { let priority = match &message.content { content if content.contains("ERROR") => Priority::Critical, content if content.contains("WARNING") => Priority::High, content if content.contains("DEBUG") => Priority::Medium, _ => Priority::Low, }; tx.send(Message { priority, ..message }) .unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx.iter() .map(|message| { format!( "[{}|{}] {}", message.priority, message.sender_id, message.content ) }) .collect() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{Receiver, Sender, channel};use std::thread::{JoinHandle, spawn};pub enum Priority { Low, Medium, High, Critical,}impl std::fmt::Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { let s = match self { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", }; write!(f, "{s}") } }pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel spawn(move || { for message in messages { let priority = match &message.content { m if m.starts_with("ERROR") => Priority::Critical, m if m.starts_with("WARNING") => Priority::High, m if m.starts_with("DEBUG") => Priority::Medium, _ => Priority::Low }; tx.send(Message { priority, ..message }).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages spawn(move || { let mut res = Vec::new(); for message in rx { res.push(format!("[{}|{}] {}", message.priority, message.sender_id, message.content)); } res })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn( move || { for message in messages { let pr = if message.content.contains("ERROR") { Priority::Critical } else if message.content.contains("WARNING") { Priority::High } else if message.content.contains("DEBUG") { Priority::Medium } else { Priority::Low }; tx.send( Message { content: message.content, sender_id: message.sender_id, priority: pr, } ); } } )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn( move || { let mut v = Vec::new(); while let Ok(msg) = rx.recv() { let pr = match msg.priority { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", }; v.push(format!("[{}|{}] {}", pr, msg.sender_id, msg.content)) } v } )}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt::{Display, Formatter, Result};pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut Formatter<'_>) -> Result { match self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT") } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel() // 1. Implement this function to create and return a message channel}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for mut msg in messages { msg.priority = if msg.content.starts_with("DEBUG") { Priority::Medium } else if msg.content.starts_with("WARNING") { Priority::High } else if msg.content.starts_with("ERROR") { Priority::Critical } else { Priority::Low }; let _ = tx.send(msg); } }) // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || { let mut result = Vec::new(); while let Ok(msg) = rx.recv() { result.push(format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)); } result }) // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt::Display;pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut message in messages { if message.content.starts_with("ERROR") { message.priority = Priority::Critical; } else if message.content.starts_with("WARNING") { message.priority = Priority::High; } else if message.content.starts_with("DEBUG") { message.priority = Priority:: Medium; } else { message.priority = Priority::Low; } tx.send(message).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut messages = Vec::new(); while let Ok(message) = rx.recv() { messages.push(format!( "[{}|{}] {}", message.priority, message.sender_id, message.content )); }; messages }) // thread::spawn(move || { // let mut messages = Vec::new(); // while let Ok(message) = rx.recv() { // messages.push(format!( // "[{}|{}] {}", // message.priority, message.sender_id, message.content // )); // } // messages}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{Receiver, Sender};use std::thread::JoinHandle;pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match &self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT"), } }}impl From<&str> for Priority { fn from(value: &str) -> Self { match value { _ if value.contains("ERROR") => Priority::Critical, _ if value.contains("WARNING") => Priority::High, _ if value.contains("DEBUG") => Priority::Medium, _ => Priority::Low, } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { std::sync::mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { std::thread::spawn(move || { for message in messages { tx.send(Message { priority: message.content.as_str().into(), ..message }) .expect("error sending") } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { std::thread::spawn(move || { rx.iter() .map(|msg| format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)) .collect() }) // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt::{Display, Formatter, Result};pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut Formatter<'_>) -> Result { let priority = match self { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", }; write!(f, "{priority}") }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}fn get_priority_from_content(content: &str) -> Priority { for (prefix, priority) in [ ("ERROR: ", Priority::Critical), ("WARNING: ", Priority::High), ("DEBUG: ", Priority::Medium), ] { if content.starts_with(prefix) { return priority; } } Priority::Low}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for mut message in messages { message.priority = get_priority_from_content(&message.content); tx.send(message).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || { let mut messages = Vec::new(); while let Ok(message) = rx.recv() { messages.push(format!( "[{}|{}] {}", message.priority, message.sender_id, message.content )); } messages })}pub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MEDIUM"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRITICAL"), } }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { let updated_messages = messages .into_iter() .map(|mut msg| { let splits: Vec<&str> = msg.content.split(":").collect(); match *splits.first().unwrap() { "ERROR" => msg.priority = Priority::Critical, "WARNING" => msg.priority = Priority::High, "DEBUG" => msg.priority = Priority::Medium, _ => msg.priority = Priority::Low, } msg }) .collect::<Vec<Message>>(); for message in updated_messages.into_iter() { tx.send(message).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || { let mut formatted_messages: Vec<String> = Vec::new(); while let Ok(msg) = rx.recv() { let msg_formated = format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content); formatted_messages.push(msg_formated); } formatted_messages })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut m in messages { if m.content.contains("ERROR") { m.priority = Priority::Critical; } else if m.content.contains("WARNING") { m.priority = Priority::High; } else if m.content.contains("DEBUG") { m.priority = Priority::Medium; } else { m.priority = Priority::Low; } tx.send(m); }; })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut msg_list: Vec<String> = Vec::new(); while let Ok(msg) = rx.recv() { match msg.priority { Priority::Critical => msg_list.push(format!("[CRIT|{}] {}",msg.sender_id,msg.content)), Priority::High => msg_list.push(format!("[HIGH|{}] {}",msg.sender_id,msg.content)), Priority::Medium => msg_list.push(format!("[MED|{}] {}",msg.sender_id,msg.content)), Priority::Low => msg_list.push(format!("[LOW|{}] {}",msg.sender_id,msg.content)), } } msg_list })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt::{Display, Formatter};pub enum Priority { Low, Medium, High, Critical,}impl Priority { pub fn as_str(&self) -> &'static str { match self { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", } }}impl Display for Priority { fn fmt(&self, f: &mut Formatter) -> std::fmt::Result { write!(f, "{}", self.as_str()) }}impl From<&str> for Priority { fn from(s: &str) -> Self { let msg = s.to_lowercase(); let msg = msg.as_str(); match msg { msg if msg.starts_with("error") => Priority::Critical, msg if msg.starts_with("warning") => Priority::High, msg if msg.starts_with("debug") => Priority::Medium, _ => Priority::Low, } }}impl From<String> for Priority { fn from(s: String) -> Self { Priority::from(s.as_str()) }}impl From<&String> for Priority { fn from(s: &String) -> Self { Priority::from(s.as_str()) }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut msg in messages.into_iter() { msg.priority = Priority::from(&msg.content); tx.send(msg).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx .iter() .map(|message| { let priority = message.priority.as_str(); let output = format!("[{}|{}] {}", priority, message.sender_id, message.content); output }) .collect::<Vec<String>>() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::{Display, Formatter};use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl Priority { pub fn by_content(msg: &str) -> Priority { if msg.starts_with("ERROR") { Priority::Critical } else if msg.starts_with("WARNING") { Priority::High } else if msg.starts_with("DEBUG") { Priority::Medium } else { Priority::Low } }}impl Display for Priority { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { match self { Priority::Critical => write!(f, "CRIT"), Priority::High => write!(f, "HIGH"), Priority::Medium => write!(f, "MED"), Priority::Low => write!(f, "LOW"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut msg in messages.into_iter() { msg.priority = Priority::by_content(msg.content.as_str()); tx.send(msg).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut output = Vec::new(); while let Ok(msg) = rx.recv() { output.push(format!( "[{}|{}] {}", msg.priority, msg.sender_id, msg.content )); } output })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt;#[derive(Clone)]pub enum Priority { Low, Medium, High, Critical,}impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", match self { Self::Low => "LOW", Self::Medium => "MED", Self::High => "HIGH", Self::Critical => "CRIT", }) }}#[derive(Clone)]pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || messages.into_iter().for_each(|mut x| { match x.content.as_str().split(':').next().unwrap() { "ERROR" => x.priority = Priority::Critical, "WARNING" => x.priority = Priority::High, "DEBUG" => x.priority = Priority::Medium, _ => x.priority = Priority::Low, } tx.send(x).unwrap(); }))}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages let mut vector = vec![]; while let Ok(msg) = rx.recv() { vector.push(format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)); } thread::spawn(move||vector)}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{Receiver, Sender, channel};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl Priority { fn by_content(content: &str) -> Self { if content.starts_with("ERROR:") { Self::Critical } else if content.starts_with("WARNING:") { Self::High } else if content.starts_with("DEBUG:") { Self::Medium } else { Self::Low } }}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let priority = match self { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", }; write!(f, "{priority}") }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for mut msg in messages.into_iter() { msg.priority = Priority::by_content(&msg.content); tx.send(msg).expect("Unable to send on channel"); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || { let mut output = Vec::new(); while let Ok(msg) = rx.recv() { output.push(format!( "[{}|{}] {}", msg.priority, msg.sender_id, msg.content )); } output })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl std::fmt::Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel<Message>() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel() }pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut msg in messages { if msg.content.contains("ERROR") { msg.priority = Priority::Critical; } else if msg.content.contains("WARNING") { msg.priority = Priority::High; } else if msg.content.contains("DEBUG") { msg.priority = Priority::Medium; } else { msg.priority = Priority::Low; }; tx.send(msg).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut formatted_messages = Vec::new(); while let Ok(message) = rx.recv() { formatted_messages.push(format!("[{}|{}] {}", message.priority, message.sender_id, message.content)); } formatted_messages })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for mut message in messages.into_iter() { if message.content.starts_with("ERROR") { message.priority = Priority::Critical } else if message.content.starts_with("WARNING") { message.priority = Priority::High } else if message.content.starts_with("DEBUG") { message.priority = Priority::Medium } else { message.priority = Priority::Low } tx.send(message).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut result = vec![]; while let Ok(msg) = rx.recv() { result.push(format!( "[{}|{}] {}", match msg.priority { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", }, msg.sender_id, msg.content )); } result })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MEDIUM"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRITICAL"), } }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel let (tx, rx) = mpsc::channel(); (tx, rx)}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut msg in messages { if msg.content.contains("ERROR") { msg.priority = Priority::Critical; } else if msg.content.contains("WARNING") { msg.priority = Priority::High; } else if msg.content.contains("DEBUG") { msg.priority = Priority::Medium; } else { msg.priority = Priority::Low; }; tx.send(msg).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut result = vec![]; while let Ok(msg) = rx.recv() { result.push(format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)) }; result })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{spawn, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl From<String> for Priority { fn from(text: String) -> Priority { if text.contains("ERROR") {return Priority::Critical;} if text.contains("WARNING") {return Priority::High;} if text.contains("DEBUG") {return Priority::Medium;} Priority::Low }}impl From<Priority> for String { fn from(priority: Priority) -> String { match priority { Priority::Low => "LOW".to_owned(), Priority::Medium => "MED".to_owned(), Priority::High => "HIGH".to_owned(), Priority::Critical => "CRIT".to_owned(), } }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel spawn(move || messages.iter().for_each(|m| tx.send( Message { content: m.content.clone(), sender_id: m.sender_id, priority: m.content.clone().into() } ).unwrap()))}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages let mut messages: Vec<String> = vec![]; while let Ok(message) = rx.recv() { messages.push( format!( "[{}|{}] {}", String::from(message.priority), message.sender_id, message.content ) ); } spawn(move || messages)}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl std::fmt::Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for mut message in messages.into_iter() { if message.content.contains("ERROR") { message.priority = Priority::Critical; } else if message.content.contains("WARNING") { message.priority = Priority::High; } else if message.content.contains("DEBUG") { message.priority = Priority::Medium; } else { message.priority = Priority::Low; } let _ = tx.send(message); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || { let mut formatted_msgs = Vec::new(); while let Ok(msg) = rx.recv() { let formatted_msg = format!( "[{}|{}] {}", msg.priority, msg.sender_id, msg.content ); formatted_msgs.push(formatted_msg); } formatted_msgs })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};#[derive(PartialEq)]pub enum Priority { Low, Medium, High, Critical,}impl Priority { pub fn abbr(&self) -> &str { match *self { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl Message { pub fn fix_priority(&mut self) { if self.content.contains("ERROR") /* || self.priority == Priority::Critical */ { self.priority = Priority::Critical; } else if self.content.contains("WARNING") /* || self.priority == Priority::High */ { self.priority = Priority::High; } else if self.content.contains("DEBUG") /* || self.priority == Priority::Medium */ { self.priority = Priority::Medium; } else { self.priority = Priority::Low; } }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || for mut msg in messages.into_iter() { msg.fix_priority(); tx.send(msg).unwrap(); } )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut results = vec![]; while let Ok(msg) = rx.recv() { results.push(format!("[{}|{}] {}", msg.priority.abbr(), msg.sender_id, msg.content)); } results })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};#[derive(Debug)]pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match *self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MEDIUM"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRITICAL"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for message in messages { let _ = tx.send(message); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { let thread_join_handle = thread::spawn(move || { let mut msg_vec = vec![]; while let Ok(msg) = rx.recv() { if let Some(first_word) = msg.content.split_whitespace().next() { let priority = match first_word { "ERROR:" => Priority::Critical, "WARNING:" => Priority::High, "DEBUG:" => Priority::Medium, _ => Priority::Low, }; msg_vec.push(format!("[{}|{}] {}", priority, msg.sender_id, msg.content)); }; } msg_vec }); thread_join_handle}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { messages.into_iter().map(|mut message| { message.priority = Priority::Low; if message.content.starts_with("ERROR:") { message.priority = Priority::Critical; } else if message.content.starts_with("WARNING:") { message.priority = Priority::High; } else if message.content.starts_with("DEBUG:") { message.priority = Priority::Medium; } message }).for_each(|message| { tx.send(message).unwrap(); }) })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx.iter().map(|message| { let priority = match message.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; let sender_id = message.sender_id; let content = message.content; format!("[{priority}|{sender_id}] {content}").to_string() }).collect::<Vec<String>>() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut message in messages { message.priority = if message.content.starts_with("ERROR") { Priority::Critical } else if message.content.starts_with("WARNING") { Priority::High } else if message.content.starts_with("DEBUG") { Priority::Medium } else { Priority::Low }; tx.send(message).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx.iter() .map(|message| { let p = match message.priority { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT", }; format!("[{}|{}] {}", p, message.sender_id, message.content) }) .collect() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};#[derive(Debug)]pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut m in messages { m.priority = match m.content.split_once(':') { Some((prefix, _)) => match prefix.trim() { "ERROR" => Priority::Critical, "WARNING" => Priority::High, "DEBUG" => Priority::Medium, _ => Priority::Low, }, None => Priority::Low, }; tx.send(m).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut messages = Vec::new(); for message in rx { let priority = match message.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MEDIUM", Priority::Low => "LOW", }; let msg_info = format!("[{}|{}]{}", priority, message.sender_id, message.content); messages.push(msg_info); } messages })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { messages.into_iter().for_each(|mut x| { x.priority = match x.content.split_once(":") { Some((bef, _aft)) => { match bef.trim() { "ERROR" => Priority::Critical, "WARNING" => Priority::High, "DEBUG" => Priority::Medium, _ => Priority::Low, } }, None => Priority::Low, }; tx.send(x).unwrap(); }); })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut msgs = Vec::new(); rx.iter().for_each(|x| { let p = match x.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; let msg = format!("[{}|{}] {}", p, x.sender_id, x.content); msgs.push(msg); }); msgs })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channels return mpsc::channel::<Message>();}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { messages.into_iter().for_each(|mut x| { match x.content.split_once(":"){ Some((before, _after)) => { match before.trim() { "ERROR" => x.priority = Priority::Critical, "WARNING" => x.priority = Priority::High, "DEBUG" => x.priority = Priority::Medium, _ => x.priority = Priority::Low } } None => x.priority = Priority::Low } tx.send(x).unwrap(); }); })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut msgs = Vec::new(); rx.iter().for_each(|x| { let c = match x.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; let msg = format!("[{}|{}] {}", c, x.sender_id, x.content); msgs.push(msg); }); msgs })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channels return mpsc::channel::<Message>();}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { messages.into_iter().for_each(|mut x| { match x.content.split_once(":"){ Some((before, _after)) => { match before.trim() { "ERROR" => x.priority = Priority::Critical, "WARNING" => x.priority = Priority::High, "DEBUG" => x.priority = Priority::Medium, _ => x.priority = Priority::Low } } None => x.priority = Priority::Low } tx.send(x).unwrap(); }); })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut msgs = Vec::new(); rx.iter().for_each(|x| { let c = match x.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; let msg = format!("[{}|{}] {}", c, x.sender_id, x.content); msgs.push(msg); }); msgs })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel return mpsc::channel::<Message>();}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || -> () { for mut i in messages { i.priority = if i.content.starts_with("ERROR") { Priority::Critical }else if i.content.starts_with("WARNING"){ Priority::High }else if i.content.starts_with("DEBUG"){ Priority::Medium }else{ Priority::Low }; tx.send(i).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || -> Vec<String> { let mut res = Vec::new(); for i in rx.iter() { let p = match i.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; res.push(format!("[{}|{}] {}",p,i.sender_id,i.content)) } return res; })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel return mpsc::channel::<Message>();}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || -> () { for mut i in messages { i.priority = if i.content.starts_with("ERROR") { Priority::Critical }else if i.content.starts_with("WARNING"){ Priority::High }else if i.content.starts_with("DEBUG"){ Priority::Medium }else{ Priority::Low }; tx.send(i).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || -> Vec<String> { let mut res = Vec::new(); for i in rx.iter() { let p = match i.priority { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; res.push(format!("[{}|{}] {}",p,i.sender_id,i.content)) } return res; })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt;pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}impl From<&String> for Priority { fn from(content: &String) -> Self { match content { c if c.starts_with("ERROR") => Priority::Critical, c if c.starts_with("WARNING") => Priority::High, c if c.starts_with("DEBUG") => Priority::Medium, _ => Priority::Low, } }}impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Priority::Critical => write!(f, "CRIT"), Priority::High => write!(f, "HIGH"), Priority::Medium => write!(f, "MED"), Priority::Low => write!(f, "LOW"), } }}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn( move || { for mut msg in messages { msg.priority = Priority::from(&msg.content); tx.send(msg).unwrap(); } } )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn( move || { let mut entries = vec![]; while let Ok(msg) = rx.recv() { let entry: String = format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content); entries.push(entry); } entries } )}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender, channel};use std::thread::{self, JoinHandle};use std::fmt;pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel let (sender, receiver) = channel(); (sender, receiver)}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut message in messages { message.priority = match message.content.as_str() { s if s.starts_with("ERROR") => Priority::Critical, s if s.starts_with("WARNING") => Priority::High, s if s.starts_with("DEBUG") => Priority::Medium, _ => Priority::Low, }; let _ = tx.send(message).unwrap(); } })}impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match self { Priority::Critical => "CRIT", Priority::High => "HIGH", Priority::Medium => "MED", Priority::Low => "LOW", }; return write!(f, "{}", s); }}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut handle: Vec<String> = Vec::new(); while let Ok(msg) = rx.recv() { handle.push(format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)); } handle })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender, channel};use std::thread::{self, JoinHandle};use std::fmt::Display;pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { Self::Critical => write!(f, "CRIT"), Self::High => write!(f, "HIGH"), Self::Medium => write!(f, "MED"), Self::Low => write!(f, "LOW"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || { for mut m in messages { if m.content.contains("ERROR") { m.priority = Priority::Critical; } else if m.content.contains("WARNING") { m.priority = Priority::High; } else if m.content.contains("DEBUG") { m.priority = Priority::Medium; } else { m.priority = Priority::Low; } tx.send(m).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || { let mut result = vec![]; for m in rx { let line = format!("[{}|{}] {}", m.priority, m.sender_id, m.content); result.push(line); } return result; })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { Self::Critical => write!(f, "CRIT"), Self::High => write!(f, "HIGH"), Self::Medium => write!(f, "MED"), Self::Low => write!(f, "LOW"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel let h = thread::spawn(move || { for mut msg in messages { if msg.content.starts_with("ERROR") { msg.priority = Priority::Critical; } else if msg.content.starts_with("WARNING") { msg.priority = Priority::High; } else if msg.content.starts_with("DEBUG") { msg.priority = Priority::Medium; } else { msg.priority = Priority::Low; } tx.send(msg).unwrap(); } }); h}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages let h = thread::spawn(move || { let mut formatted_messages = Vec::new(); for msg in rx { let formatted = format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content); formatted_messages.push(formatted); } formatted_messages }); h}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};#[derive(Debug)]pub enum Priority { Low, Medium, High, Critical,}impl Priority { fn display_name(&self) -> &'static str { match self { Priority::Low => "LOW", Priority::Medium => "MEDIUM", Priority::High => "HIGH", Priority::Critical => "CRIT", } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel // - Receives a vector of messages and a sender channel let handle = thread::spawn(move || { for message in messages { let priority = if message.content.starts_with("ERROR") { Priority::Critical } else if message.content.starts_with("WARNING") { Priority::High } else if message.content.starts_with("DEBUG") { Priority::Medium } else { Priority::Low }; let updated_message = Message { priority, ..message }; tx.send(updated_message).unwrap(); } }); handle}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages let handle = thread::spawn(move || { let mut formatted_messages = Vec::new(); while let Ok(message) = rx.recv() { let formatted_message = format!( "[{}|{}] {}", // Use display_name method for priority format message.priority.display_name(), message.sender_id, message.content ); formatted_messages.push(formatted_message); } formatted_messages }); handle}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::fmt::Display;use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { Self::Critical => write!(f, "CRIT"), Self::High => write!(f, "HIGH"), Self::Medium => write!(f, "MED"), Self::Low => write!(f, "LOW"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut msg in messages { if msg.content.starts_with("ERROR") { msg.priority = Priority::Critical; } else if msg.content.starts_with("WARNING") { msg.priority = Priority::High; } else if msg.content.starts_with("DEBUG") { msg.priority = Priority::Medium; } else { msg.priority = Priority::Low; } let _ = tx.send(msg); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut formatted: Vec<String> = Vec::new(); for msg in rx { formatted.push(format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)); } formatted })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl From<&str> for Priority { fn from(string: &str) -> Self { match string.trim() { trimmed if trimmed.starts_with("ERROR") => Priority::Critical, trimmed if trimmed.starts_with("WARNING") => Priority::High, trimmed if trimmed.starts_with("DEBUG") => Priority::Medium, _ => Priority::Low, } } }impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl Message { fn update_priority(self) -> Self { Message { priority: self.content.as_str().into(), ..self } }}impl From<Message> for String { fn from(message: Message) -> Self { format!("[{}|{}] {}", message.priority, message.sender_id, message.content) }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { thread::spawn(move || messages .into_iter() .for_each(|message| { tx.send(message.update_priority()).unwrap(); }) )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { thread::spawn(move || rx .into_iter() .map(|message| message.into()) .collect() )}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt::Display;pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { Self::Critical => write!(f, "CRIT"), Self::High => write!(f, "HIGH"), Self::Medium => write!(f, "MED"), Self::Low => write!(f, "LOW"), } }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut msg in messages { if msg.content.starts_with("ERROR") { msg.priority = Priority::Critical; } else if msg.content.starts_with("WARNING") { msg.priority = Priority::High; } else if msg.content.starts_with("DEBUG") { msg.priority = Priority::Medium; } else { msg.priority = Priority::Low; } let _ = tx.send(msg); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut formatted: Vec<String> = Vec::new(); for received in rx { formatted.push(format!("[{}|{}] {}", received.priority, received.sender_id, received.content)); } formatted })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt;pub enum Priority { Low, Medium, High, Critical,}impl fmt::Display for Priority { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let s = match self { Priority::Low => "LOW", Priority::Medium => "MED", Priority::High => "HIGH", Priority::Critical => "CRIT" }; write!(f, "{}", s) }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { messages.into_iter().for_each(|mut item| { let priority = if item.content.contains("ERROR") {Priority::Critical} else if item.content.contains("WARNING") {Priority::High} else if item.content.contains("DEBUG") {Priority::Medium} else { Priority::Low }; item.priority = priority; tx.send(item); }) })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut result = vec![]; while let Ok(msg) = rx.recv() { let s = format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content); result.push(s); }; result })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel std::thread::spawn( move || { for mut m in messages.into_iter() { if m.content.contains("ERROR") { m.priority = Priority::Critical; } else if m.content.contains("WARNING") { m.priority = Priority::High; } else if m.content.contains("DEBUG") { m.priority = Priority::Medium; } else { m.priority = Priority::Low; } let _ = tx.send(m); } } )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages std::thread::spawn( move || { let mut out: Vec<String> = vec![]; while let Ok(msg) = rx.recv() { match msg.priority { Priority::Low => { let s = format!("[LOW|{}] {}", msg.sender_id, msg.content); out.push(s); } Priority::Medium => { let s = format!("[MED|{}] {}", msg.sender_id, msg.content); out.push(s); } Priority::High => { let s = format!("[HIGH|{}] {}", msg.sender_id, msg.content); out.push(s); } Priority::Critical => { let s = format!("[CRIT|{}] {}", msg.sender_id, msg.content); out.push(s); } } } out } )}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel std::thread::spawn( move || { for mut m in messages.into_iter() { if m.content.contains("ERROR") { m.priority = Priority::Critical; } else if m.content.contains("WARNING") { m.priority = Priority::High; } else if m.content.contains("DEBUG") { m.priority = Priority::Medium; } else { m.priority = Priority::Low; } let _ = tx.send(m); } } )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages std::thread::spawn( move || { let mut out: Vec<String> = vec![]; while let Ok(msg) = rx.recv() { match msg.priority { Priority::Low => { let s = format!("[LOW|{}] {}", msg.sender_id, msg.content); out.push(s); } Priority::Medium => { let s = format!("[MED|{}] {}", msg.sender_id, msg.content); out.push(s); } Priority::High => { let s = format!("[HIGH|{}] {}", msg.sender_id, msg.content); out.push(s); } Priority::Critical => { let s = format!("[CRIT|{}] {}", msg.sender_id, msg.content); out.push(s); } } } out } )}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl std::fmt::Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match *self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel// Producer Thread// Receives a vector of messages and a sender channel// Updates priority based on content keywords:// "ERROR" → Critical// "WARNING" → High// "DEBUG" → Medium// Others become Low// Sends each updated message through the channel std::thread::spawn(move || { for message in messages.iter() { if message.content.contains("ERROR") { tx.send(Message { content: message.content.to_string(), sender_id: message.sender_id, priority: Priority::Critical }).unwrap(); } else if message.content.contains("WARNING") { tx.send(Message { content: message.content.to_string(), sender_id: message.sender_id, priority: Priority::High }).unwrap(); } else if message.content.contains("DEBUG") { tx.send(Message { content: message.content.to_string(), sender_id: message.sender_id, priority: Priority::Medium }).unwrap(); } else { tx.send(Message { content: message.content.to_string(), sender_id: message.sender_id, priority: Priority::Low }).unwrap(); } } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages// Consumer Thread// Receives messages until the channel is closed// Formats each message as: [PRIORITY|SENDER_ID] CONTENT where PRIORITY is one of: LOW, MED, HIGH, CRIT// Returns a vector of formatted message strings std::thread::spawn(move || { let mut result: Vec<String> = Vec::new(); while let Ok(msg) = rx.recv() { result.push(format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)); } result })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{Receiver, Sender, channel};use std::thread::{self, JoinHandle};use std::fmt::Display;pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for message in messages { for (keyword, priority) in [("ERROR", Priority::Critical), ("WARNING", Priority::High), ("DEBUG", Priority::Medium), ("", Priority::Low)] { if message.content.contains(keyword) { let _ = tx.send(Message { priority, ..message}); break; } } } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx.into_iter().map(|Message {content, sender_id, priority}| format!("[{}|{}] {}", priority, sender_id, content)).collect() })}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { match self { Self::Critical => write!(f, "CRIT"), Self::High => write!(f, "HIGH"), Self::Medium => write!(f, "MED"), Self::Low => write!(f, "LOW"), } }}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl std::fmt::Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match *self { Priority::Low => write!(f, "LOW"), Priority::Medium => write!(f, "MED"), Priority::High => write!(f, "HIGH"), Priority::Critical => write!(f, "CRIT"), } }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { messages.into_iter().for_each(|mut item| { let priority = if item.content.starts_with("ERROR") { Priority::Critical } else if item.content.starts_with("WARNING") { Priority::High } else if item.content.starts_with("DEBUG") { Priority::Medium } else { Priority::Low }; item.priority = priority; tx.send(item).unwrap(); }); })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut formatted_messages = Vec::new(); while let Ok(message) = rx.recv() { formatted_messages.push(format!( "[{}|{}] {}", message.priority, message.sender_id, message.content )); } formatted_messages })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt::Display;pub enum Priority { Low, Medium, High, Critical,}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match *self { Priority::Critical => write!(f, "CRIT"), Priority::High => write!(f, "HIGH"), Priority::Medium => write!(f, "MED"), _ => write!(f, "LOW") } }}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut message in messages { if message.content.starts_with("ERROR") { message.priority = Priority::Critical; } else if message.content.starts_with("WARNING") { message.priority = Priority::High; } else if message.content.starts_with("DEBUG") { message.priority = Priority::Medium; } else { message.priority = Priority::Low; } tx.send(message); } } )}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut messages = Vec::new(); for msg in rx { messages.push(format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content)); } messages })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::fmt::Display;use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match *self { Priority::Critical => write!(f, "CRIT"), Priority::High => write!(f, "HIGH"), Priority::Medium => write!(f, "MED"), Priority::Low => write!(f, "LOW"), } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel::<Message>()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { messages.into_iter().for_each(|mut m| { let priority = if m.content.starts_with("ERROR") { Priority::Critical } else if m.content.starts_with("WARNING") { Priority::High } else if m.content.starts_with("DEBUG") { Priority::Medium } else { Priority::Low }; m.priority = priority; tx.send(m).unwrap() }) })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { rx.into_iter() .map(|m| { format!("[{}|{}] {}", m.priority, m.sender_id, m.content) }) .collect() })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}
use std::sync::mpsc::{self, Receiver, Sender};use std::thread::{self, JoinHandle};use std::fmt::{Display, Formatter, Result};pub enum Priority { Low, Medium, High, Critical,}impl Display for Priority { fn fmt(&self, f: &mut Formatter) -> Result { match self { Priority::Critical => write!(f, "CRIT"), Priority::High => write!(f, "HIGH"), Priority::Medium => write!(f, "MED"), _ => write!(f, "LOW") } }}pub struct Message { pub content: String, pub sender_id: u32, pub priority: Priority,}pub fn create_message_channel() -> (Sender<Message>, Receiver<Message>) { // 1. Implement this function to create and return a message channel mpsc::channel()}pub fn create_producer_thread(messages: Vec<Message>, tx: Sender<Message>) -> JoinHandle<()> { // TODO: Create a thread that: // - Updates the priority based on content // - Sends the updated message through the channel thread::spawn(move || { for mut message in messages { if message.content.contains("ERROR") { message.priority = Priority::Critical; } else if message.content.contains("WARNING") { message.priority = Priority::High; } else if message.content.contains("DEBUG") { message.priority = Priority::Medium; } else { message.priority = Priority::Low; } tx.send(message).unwrap(); } })}pub fn create_consumer_thread(rx: Receiver<Message>) -> JoinHandle<Vec<String>> { // TODO: Create a thread that: // - Receives messages from the channel // - Formats them as "[PRIORITY|SENDER_ID] CONTENT" // - Returns a vector of formatted messages thread::spawn(move || { let mut results = vec![]; while let Ok(msg) = rx.recv() { let formatted_message = format!("[{}|{}] {}", msg.priority, msg.sender_id, msg.content); results.push(formatted_message); } results })}// Example Usagepub fn main() { let (tx, rx) = create_message_channel(); let mut producer_handles = vec![]; for id in 0..3 { let tx_clone = tx.clone(); let messages = vec![ Message { content: format!("Normal message from producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("WARNING: System running hot on producer {}", id), sender_id: id, priority: Priority::Low, }, Message { content: format!("ERROR: Connection lost on producer {}", id), sender_id: id, priority: Priority::Low, }, ]; let handle = create_producer_thread(messages, tx_clone); producer_handles.push(handle); } drop(tx); let consumer_handle = create_consumer_thread(rx); for handle in producer_handles { handle.join().unwrap(); } let results = consumer_handle.join().unwrap(); println!("Processed messages:"); for msg in results { println!("{}", msg); }}