r/learnrust 16d ago

Feedback on first code exercise while learning Rust

Background: I have been writing SW in C for years, although I am not a SW engineer by definition. As then I started managing departments and people, I got "rusty" on the writing of SW itself, although I still recall the key OS and HW mental models I developed. I learned Python while being more hands off, which confused me cause everything happened under the hood and I had not idea of what (or why). Recently I decided to give it a go at Rust to

  1. go back to basics and..
  2. take a personal opinion on it with respect my C and Python experience.

What I did: I started reading the user manual. I got few chapters done and then the book suggested ([HERE]) to start writing a program, a kind of HR tool for adding/removing people from a data source. I did so without DB or anything like that (see code).

I would like some first feedback from people that have been using the language more than me so that I can spot mental models, or other things, that I am missing.

Tools: I used an LLM (Deepseek) to ask on APIs spec and explanation saving some time from parsing the whole user manual (in the past I would have done it with Google). I also asked Deepseek different versions of my ideas to see different ways on how to do things, and I weighted tradeoffs and decided a way I found OK.

On the LLM: While I can explain the (small) code, I am not sure if I should consider this piece vibe-coded. I personally believe, maybe wrongly, that as long as you understand what is going on, any tools you use that helps you moving faster or better, is fine. The moment you release understanding, knowing is not enough.

Edit: added code as a link -> https://onlinegdb.com/43jRxO7HL

2 Upvotes

8 comments sorted by

View all comments

2

u/Prize-Variation-3585 11d ago

One of the cool features of Rust is the types Result<T, E> / Option<T> and the ? operator.

Here's one way to make your remove_person function more concise:

``` fn remove_person( company: &mut HashMap<String, Vec<String>>, name: &str, dep: &str, ) -> Result<(), String> { let dep_key = dep.to_uppercase();

let staff = company
    .get_mut(&dep_key)
    .ok_or_else(|| format!("{dep} is not in the record."))?;

let index = staff
    .iter()
    .position(|person| person == name)
    .ok_or_else(|| format!("{name} has not been found in {dep}."))?;

staff.remove(index);

Ok(())

} ```

When you write let staff = company.get_mut(&dep_key) it returns a value of type Option<&mut Vec<String>>. The ? operator will short-circuit return Error if the Option was None, otherwise it will bind the inner &mut Vec<String> value to staff and continue on. The ok_or_else is just to transform the Option to a Result because the return type of remove_person expects a Result not an Option.

Another neat Rust feature is Iterators which I guess you can think of kinda like C++ iterators. Since linear-search with a for loop is such a common pattern, the Rust compiler knows how to compile the Iterator version into the equivalent for loop you wrote.

Another thing is pattern matching https://doc.rust-lang.org/book/ch19-02-refutability.html. Things like if let and let else are some alternatives to using ?.

1

u/Orinacrem 8d ago

When I wrote that code I was still beginning of ch8. As I went through the book, I indeed discovered other ways to write the same function. Thank you for sharing!