Rust — Median, Mode, Strings and Collections Manipulation

Joshua Etim
Operations Research Bit
1 min readAug 22, 2023

I’m loving the Rust programming language. It challenges my general approach to programming by forcing me to think in new terms. The Rust Book poses some interesting challenges at the end of Chapter 8. This short brief is just my solutions to the exercises.

Question: Given a list of integers, use a vector and return the median (when sorted, the value in the middle position) and mode (the value that occurs most often; a hash map will be helpful here) of the list.

Answer:

Question: Convert strings to pig latin. The first consonant of each word is moved to the end of the word and “ay” is added, so “first” becomes “irst-fay.” Words that start with a vowel have “hay” added to the end instead (“apple” becomes “apple-hay”). Keep in mind the details about UTF-8 encoding!

Answer:

(Got the first letter extraction idea from Shepmaster’s answer here)

Question: Using a hash map and vectors, create a text interface to allow a user to add employee names to a department in a company. For example, “Add Sally to Engineering” or “Add Amir to Sales.” Then let the user retrieve a list of all people in a department or all people in the company by department, sorted alphabetically. (alphabetically not covered, yet)

Answer:

Please let me know if you have any ideas to improve these solutions. It will be much appreciated. Thanks.

Links

  1. The Rust Book: https://doc.rust-lang.org/book/ch08-03-hash-maps.html
  2. Chapter 8 exercises: https://doc.rust-lang.org/book/ch08-03-hash-maps.html#summary

--

--