You've already forked rust-tutor
second exercise!
- create exercise string manipulator (shout out claude) - finish hacky solution (it almost works)
This commit is contained in:
30
exercises/string-manipulator/string-manipulator-exercise.md
Normal file
30
exercises/string-manipulator/string-manipulator-exercise.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Exercise: String Manipulator Tool
|
||||
|
||||
**Concepts practiced:** String vs &str, ownership/borrowing, Vec operations, pattern matching
|
||||
**Objective:** Build a CLI tool that performs various string operations
|
||||
|
||||
## Requirements:
|
||||
- Create a `StringTool` struct that holds a collection of strings
|
||||
- Implement methods:
|
||||
- `new()` - creates empty tool
|
||||
- `add_string(&mut self, s: String)` - adds string to collection
|
||||
- `longest(&self) -> Option<&str>` - returns longest string (or None if empty)
|
||||
- `contains_word(&self, word: &str) -> Vec<&str>` - returns strings containing the word
|
||||
- `to_uppercase_all(&mut self)` - converts all strings to uppercase in-place
|
||||
|
||||
## Success criteria:
|
||||
- Code compiles without warnings
|
||||
- Test with: adding "hello", "world", "rust programming"
|
||||
- `longest()` should return "rust programming"
|
||||
- `contains_word("o")` should return vec!["hello", "world", "rust programming"]
|
||||
- After `to_uppercase_all()`, all strings should be uppercase
|
||||
|
||||
## Stretch goal:
|
||||
Add a method `word_count(&self) -> HashMap<String, usize>` that counts total occurrences of each word across all strings
|
||||
|
||||
## Getting Started:
|
||||
1. `cd exercises && cargo new string-manipulator`
|
||||
2. Focus on ownership: when do you need `String` vs `&str`?
|
||||
3. Think about borrowing: methods that read vs modify
|
||||
|
||||
Expected time: 45-60 minutes
|
||||
Reference in New Issue
Block a user