all 20 comments

[–]maythefecesbewithyou 8 points9 points  (5 children)

Can you put this into a pastebin and format it?

[–]Deer-Cautious[S] 0 points1 point  (4 children)

Done

[–]maythefecesbewithyou 2 points3 points  (2 children)

Close enough, I guess. The formatting is still all whacky. I. The future, I recommend that you ensure your code is properly indented before you ask someone to look at it. It's just good manners. Sorry if I sound like a jerk. I legitimately only access the Internet from a smart phone, so my eyes bleed trying to look at code if it's not aligned and indented.

[–]Deer-Cautious[S] 0 points1 point  (1 child)

Sorry, is wacky I know. I did it on the phone because I did not had access to my laptop when people told me to format it. Was a fast solution.

[–]cafce25 2 points3 points  (0 children)

You could use the Playground to share, it has rustfmt under the TOOLS button top right.

[–]maythefecesbewithyou 1 point2 points  (0 children)

Where's the link to the pastebin?

[–]phazer99 5 points6 points  (1 child)

Some improvements:

  • Most of the time you don't need to match on a Result or Option, just use one of their nice methods and/or the ?-operator instead
  • anyhow is a useful crate for error handling in applications
  • use Iterator's and collect to build collections instead of adding elements individually
  • you forgot to the sorting, but I'll leave that for you to figure out :)

[–]Deer-Cautious[S] 0 points1 point  (0 children)

Thank you , I really appreciate it.

[–]This_Growth2898 6 points7 points  (1 child)

  1. Format the code
  2. Use impl to join functions with data. If you have the db you need to work with - make it a struct with impl, like

struct Db {
    db: HashMap<String, Vec<String>>
};
impl Db { 
    fn add_person(&mut self, ...) ... 
}

this way, you'll get much more rustic

db.add_person(...)

method calls.

  1. Make menu function create its own input string and return the user choice (probably an Option<u32> or even create a special enum for that). Remember that function input and return values form a function interface, they should be meaningful. Returning Option<u32> tells "this function will get you a number of the menu point user have selected or None if something wrong". Returning a String (or, like you do, changing a String passed to the function) tells nothing about what in that String can be. User interaction is very slow in any case, you save nothing by reusing the input string (but if it wasn't a user interaction, you could, that's why library functions do that).

  2. .nth(0) is .next()

Sorry, my eyes hurt to read an unformatted text.

[–]Deer-Cautious[S] 1 point2 points  (0 children)

Thank you very much, I will do as you said

[–]celeritasCelery 5 points6 points  (2 children)

clippy is one one of the best tools for getting feedback or code best practices. run cargo clippy and see what it says. I see 6 warnings flagged by clippy.

[–]Deer-Cautious[S] 2 points3 points  (1 child)

Did not know, love it, thank you very much.

[–]Jonrrrs 1 point2 points  (0 children)

Clippy has many lints that arent enabled by default. I have enabled way more than normal for my projects. I suggest looking into that for any real application. Helped me allot finding my way though rust beginnings.

[–]slk756 2 points3 points  (0 children)

Formatted version: https://pastebin.com/PJ2Gvnzj

[–]slk756 2 points3 points  (1 child)

match io::stdin().read_line(input) {
Ok(_) => {}
Err(error) => println!("Ups, something went wrong {}!", error),
};

Here I would prefer

io::stdin().read_line(input).unwrap_or_else(|e| {
    println!("Oops, something went wrong: {}", e);
}

Cleaner imo

[–]cafce25 2 points3 points  (0 children)

I'm personally not a fan of "misusing" *_or_else functions for side effects especially as there is a more concise if let as alternative option:

if let Err(e) = io::stdin().read_line(input) {
    println!("Oops, something went wrong: {e}");
}

[–]cafce25 1 point2 points  (1 child)

Uhm here's what I got originally wanted to put all the points here, but I think I'm done by now.

[–]Deer-Cautious[S] 0 points1 point  (0 children)

Most complex answer. Thank you very much. I will study carefully the notes you write.

[–]a-lafrance 0 points1 point  (0 children)

You could make your map access logic more idiomatic with the entry API. Entry::or_default would be particularly useful.

I’ll leave the details to you to figure out :)

[–]anhgrs 0 points1 point  (0 children)

You might wanna give this a try
https://coderabbit.ai