You've already forked rust-tutor
- create exercise string manipulator (shout out claude) - finish hacky solution (it almost works)
1.3 KiB
1.3 KiB
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
StringToolstruct that holds a collection of strings - Implement methods:
new()- creates empty tooladd_string(&mut self, s: String)- adds string to collectionlongest(&self) -> Option<&str>- returns longest string (or None if empty)contains_word(&self, word: &str) -> Vec<&str>- returns strings containing the wordto_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:
cd exercises && cargo new string-manipulator- Focus on ownership: when do you need
Stringvs&str? - Think about borrowing: methods that read vs modify
Expected time: 45-60 minutes