Sure! Here are five fun Rust exercises for students who are new to programming. Each exercise is designed to be completed in about 30 to 45 minutes.
Exercise 1: Simple Calculator
Objective: Create a simple command-line calculator that can perform addition, subtraction, multiplication, and division.
Instructions:
- Prompt the user to enter two numbers and an operator (+, -, *, /).
- Based on the operator, perform the corresponding calculation.
- Print the result to the console.
Learning Outcomes:
- Understanding of basic input/output.
- Familiarity with control flow (if statements).
Exercise 2: Guess the Number
Objective: Build a guessing game where the user has to guess a randomly generated number.
Instructions:
- Generate a random number between 1 and 100.
- Prompt the user to guess the number.
- Give feedback if the guess is too high, too low, or correct.
- Allow the user to keep guessing until they find the correct number.
Learning Outcomes:
- Introduction to random number generation.
- Loops and conditionals practice.
Exercise 3: Fancy Strings
Objective: Create a program that modifies a string entered by the user in fun ways.
Instructions:
- Prompt the user to enter a string.
- Offer them options to:
- Convert to uppercase.
- Convert to lowercase.
- Reverse the string.
- Count vowels and consonants.
- Print the modified string or counts as appropriate.
Learning Outcomes:
- String manipulation and methods.
- Working with user input and output.
Exercise 4: To-Do List
Objective: Implement a simple command-line to-do list manager.
Instructions:
- Create a struct called
Task with a description and a completed flag.
- Implement functionality to:
- Add a new task.
- View all tasks.
- Mark a task as completed.
- Remove a task.
- Use arrays or vectors to store tasks.
Learning Outcomes:
- Introduction to structs and vectors.
- Basic data management techniques.
Exercise 5: Word Count
Objective: Develop a program that counts occurrences of words in a text input by the user.
Instructions:
- Prompt the user to enter multiple lines of text (use a loop to allow multiple lines until a certain input, like "done").
- Split the input into words and count the occurrences of each word.
- Print the results as a list of words with their counts.
Learning Outcomes:
- Working with collections (like HashMap for counting).
- String splitting and iteration.
Conclusion
Each of these exercises not only introduces students to fundamental programming concepts in Rust, but they also emphasize fun interactive elements that can keep students engaged and motivated to learn more. Encourage them to expand on these exercises after the initial completion for added experience!