Files
rust-tutor/exercises/string-manipulator/string-manipulator-exercise.md
Reality Enjoyer 0b910bf74d second exercise!
- create exercise string manipulator (shout out claude)
- finish hacky solution (it almost works)
2026-01-15 09:11:30 +00:00

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 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