
Practice 50 Message Passing Coding Questions
Q1. Write a Rust program to create a basic message-passing channel.
Expected Output:
Message received: "Hello"
Q2. Write a Rust program to send multiple messages through a channel from one thread to another.
Input:
Messages: "Message 1", "Message 2"
Expected Output:
Message received: "Message 1"
Message received: "Message 2"
Q3. Write a Rust program to demonstrate the use of channels with mpsc for sending and receiving data.
Input:
Sender: "Task 1", Receiver: "Task 1 completed"
Expected Output:
Task 1 completed
Q4. Write a Rust program to create a channel and use it to pass an integer from one thread to another.
Input:
Sender: 42
Expected Output:
Received number: 42
Q5. Write a Rust program to create a channel with a sender and receiver, where the sender sends strings and the receiver prints them.
Input:
Sender: "Rust", Receiver: "Programming"
Expected Output:
Received message: "Rust"
Received message: "Programming"
Q6. Write a Rust program to use a channel to send data from multiple sender threads to a single receiver thread.
Expected Output:
Received: "Message from thread 1"
Received: "Message from thread 2"
Received: "Message from thread 3"
Q7. Write a Rust program to use a channel for communication between two threads to count the number of words in a sentence.
Input:
Sentence: "Rust is fast and safe."
Expected Output:
Word count: 4
Q8. Write a Rust program to use a channel to send data between threads for calculating the factorial of a number.
Input:
Number: 5
Expected Output:
Factorial result: 120
Q9. Write a Rust program to send a custom struct through a channel from one thread to another.
Input:
Struct: Person { name: "Alice", age: 30 }
Expected Output:
Received person: Name: Alice, Age: 30
Q10. Write a Rust program to create a channel to pass a list of integers and compute their sum in a separate thread.
Input:
List: [1, 2, 3, 4, 5]
Expected Output:
Sum: 15
Q11. Write a Rust program to use a mpsc channel to send a boolean value and use it to control program flow.
Input:
Value: true
Expected Output:
Program flow allowed: true
Q12. Write a Rust program to use a channel to implement a producer-consumer scenario.
Expected Output:
Producer produced: Item 1
Consumer consumed: Item 1
Q13. Write a Rust program to use mpsc channel to send data asynchronously between threads.
Expected Output:
Asynchronous message received: "Hello from thread"
Q14. Write a Rust program to demonstrate a channel where the sender sends data of type f64 and the receiver prints it.
Input:
Sender: 3.1415
Expected Output:
Received value: 3.1415
Q15. Write a Rust program to use a channel to send a vector from one thread to another.
Input:
Vector: [1, 2, 3, 4]
Expected Output:
Received vector: [1, 2, 3, 4]
Q16. Write a Rust program to create a channel and have a thread send a result of a mathematical calculation.
Input:
Calculation: 5 6
Expected Output:
Result: 30
Q17. Write a Rust program to use a channel to send strings and count how many times a string is received.
Input:
Strings: "Hello", "Hello", "World"
Expected Output:
Received "Hello" 2 times
Received "World" 1 time
Q18. Write a Rust program to use a channel to implement a queue where multiple threads enqueue and dequeue data.
Expected Output:
Data dequeued: "Task 1"
Data dequeued: "Task 2"
Q19. Write a Rust program to use a channel for sending a result of string concatenation from one thread to another.
Input:
Strings: "Rust", "Language"
Expected Output:
Concatenated result: "RustLanguage"
Q20. Write a Rust program to use a channel to pass error messages between threads.
Input:
Error message: "An error occurred"
Expected Output:
Error received: "An error occurred"
Q21. Write a Rust program to use a channel to pass user inputs from the main thread to a worker thread.
Input:
User input: "Hello"
Expected Output:
Worker received input: "Hello"
Q22. Write a Rust program to use a channel for passing data and check whether the receiver gets data.
Input:
Message: "Ready"
Expected Output:
Receiver got message: "Ready"
Q23. Write a Rust program to create a channel that passes u32 data from a worker thread to the main thread.
Input:
Data: 100
Expected Output:
Received data: 100
Q24. Write a Rust program to use a channel to send a status update message from a worker thread.
Input:
Message: "Task in progress"
Expected Output:
Status update: "Task in progress"
Q25. Write a Rust program to use a channel where the sender and receiver exchange data until the sender signals completion.
Expected Output:
Received data: "Task 1"
Received data: "Task 2"
Sender completed.
Q26. Write a Rust program to use a channel to send and receive a tuple of (i32, i32) between threads.
Input:
Tuple: (3, 5)
Expected Output:
Received tuple: (3, 5)
Q27. Write a Rust program to use a channel to pass information on whether a file exists or not between threads.
Input:
File existence: true
Expected Output:
File exists: true
Q28. Write a Rust program to use a channel to send the result of an HTTP request (success or failure) between threads.
Expected Output:
HTTP Request result: "Success"
Q29. Write a Rust program to use a channel to pass an object of a custom type between threads.
Input:
Custom type object: User { name: "Bob", age: 28 }
Expected Output:
Received user: Name: Bob, Age: 28
Q30. Write a Rust program to use a channel to send a result of a string reversal from one thread to another.
Input:
String: "Rust"
Expected Output:
Reversed string: "tsuR"
Q31. Write a Rust program to use a channel to send a list of strings and count the total number of characters.
Input:
List: ["Rust", "Programming", "is", "fun"]
Expected Output:
Total character count: 25
Q32. Write a Rust program to pass messages between threads to simulate a worker queue.
Expected Output:
Worker 1: Task 1
Worker 2: Task 2
Q33. Write a Rust program to use a channel to send and receive a boolean value for synchronization.
Input:
Value: true
Expected Output:
Received value: true
Q34. Write a Rust program to use a channel to send data and use the receiver to process it asynchronously.
Expected Output:
Async processing: "Data received"
Q35. Write a Rust program to create a channel to send results from a computation thread.
Input:
Computation: 5 + 3
Expected Output:
Computation result: 8
Q36. Write a Rust program to use a channel to handle communication between threads in a simulation of a restaurant ordering system.
Expected Output:
Order received: "Pizza"
Order received: "Pasta"
Q37. Write a Rust program to use a channel for sending an array of integers from one thread to another.
Input:
Array: [1, 2, 3, 4, 5]
Expected Output:
Array received: [1, 2, 3, 4, 5]
Q38. Write a Rust program to use a channel for communication between threads to simulate an event notification system.
Expected Output:
Event triggered: "Button clicked"
Q39. Write a Rust program to use a channel to pass the result of a mathematical series computation between threads.
Input:
Series: 1 + 1/2 + 1/3
Expected Output:
Series result: 1.83333
Q40. Write a Rust program to create a channel and send an action request from the main thread to a worker thread.
Input:
Action: "Start task"
Expected Output:
Worker received action: Start task
Q41. Write a Rust program to use a channel to notify the main thread about the status of multiple threads.
Expected Output:
Thread 1 completed
Thread 2 completed
Q42. Write a Rust program to implement a queue-like structure using a channel.
Expected Output:
Queue item dequeued: "Task A"
Queue item dequeued: "Task B"
Q43. Write a Rust program to use a channel to send a result of a comparison between two strings.
Input:
Strings: "Rust" and "Rust"
Expected Output:
Strings match: true
Q44. Write a Rust program to send a result from multiple threads to aggregate the sum of a list of numbers.
Input:
Numbers: [10, 20, 30]
Expected Output:
Total sum: 60
Q45. Write a Rust program to use a channel to process a user's input in multiple threads.
Input:
User input: "Process this"
Expected Output:
Processed input: "Process this"
Q46. Write a Rust program to use a channel for exchanging a flag between threads for synchronization.
Input:
Flag: true
Expected Output:
Flag received: true
Q47. Write a Rust program to send a custom enum value through a channel.
Input:
Enum: Message::Start
Expected Output:
Received message: Start
Q48. Write a Rust program to use a channel to pass a tuple (String, i32) from one thread to another.
Input:
Tuple: ("Task", 5)
Expected Output:
Received tuple: ("Task", 5)
Q49. Write a Rust program to use a channel to send data and handle errors within the communication flow.
Input:
Error message: "Connection failed"
Expected Output:
Error received: Connection failed
Q50. Write a Rust program to use a channel to send and process a sequence of characters.
Input:
Characters: "A", "B", "C"
Expected Output:
Character received: "A"
Character received: "B"
Character received: "C"