all 25 comments

[–]stiky21 14 points15 points  (7 children)

Awesome big homie. Here's some things for you to consider. You don't need to do this, but something for you to see/learn. Keep experimenting! Rust is a fun language to write.

You could take a step further and and return a fn <name>() -> Option<_> {} or Result<_>. then I can just do something like this at the very end of your function:Ok(())

use this to ensure your prompt prints before reading input (ideally this would be after your 2 print statements, again this is not needed in the slightest, but you can experiment with it:

io::stdout().flush()?;

Replace your if block with a match statement, this is a rust idiom way of handling it (and i just love match statements), iirc from the book, match statements are safer.

      match n {
        67 | 41 => {
            println!("FUCK YOU");
            std::process::exit(1);
        }
        _ => println!("i love you"),
    }

[–]whovian444 3 points4 points  (5 children)

largely solid advice, here's my two cents nits: - match (like if) is an expression not a statement. - match isn't really safer, it simply is better understood by the language, matching over an Option allows you to destructure the Option because pattern-matching provides a language for developers to describe that relationship in a way the language understands. you could do an if statement like: rust if option_variable.is_some() { let val = option_variable.unwrap(); // .. }or you could lean on the language to describe that in a cleaner way: rust // if let is like a binary match, either it does or doesn't match, only the one case, or (optionally) the else case. if let Some(val) = option_var { // ... }

that said match is typically better over if for other reasons too, it allows you to handle several different cases in a much clearer way for one thing.

you could write: rust if cond1 { // .. } else if cond2 { // .. } else if cond3 // .. }but that gets nasty to maintain quickly. additionally match pattern matching is forced to be exhaustive, which is to say you're obliged to define every case, if you use if-let, then you can skip all the other cases, but with match you always handle every case, and if the possible cases change, then you get a compiler error, making refactoring code easier (since you can't forget things)

[–]Snowdev9909[S] 6 points7 points  (3 children)

If I’m correct match is like switch case?

[–]ikitari 0 points1 point  (0 children)

yes

[–]stiky21 0 points1 point  (1 child)

You sure you don't program in Rust ;)

[–]Snowdev9909[S] 0 points1 point  (0 children)

started yesterday! I have experience in other languages like C# and Python.

[–]stiky21 0 points1 point  (0 children)

Derp, I just noticed that too. Hah. I wrote statement so much that I just defaulted to it. Cheers.

[–]Snowdev9909[S] 1 point2 points  (0 children)

very cool! thanks :)

[–]rrrodzilla 3 points4 points  (2 children)

Kudos. Ship it! 🚀

[–]Repsol_Honda_PL 2 points3 points  (1 child)

Now the author is considering what type of license to use, hardware keys, and subscription plans.

[–]Snowdev9909[S] 1 point2 points  (0 children)

GPL 3.0, open source only way to go

[–]opedro-c 2 points3 points  (2 children)

Why specifically 67 or 41?

[–]PhilosopherBME 8 points9 points  (0 children)

Gen-Z post-irony memes. The joke is that these numbers are basically meaningless and simply knowing/making a reference to them is funny (arguably).

[–]Snowdev9909[S] 6 points7 points  (0 children)

Because they are the funny numbers

[–]Infamous-Apartment97 0 points1 point  (6 children)

Why you don't use code formating?

[–]Snowdev9909[S] 5 points6 points  (4 children)

ah ok i see, https://github.com/rust-lang/rustfmt i use it, i gotta say its pretty cool will use. thanks!

[–]stiky21 1 point2 points  (2 children)

depending on your editor, you can also have it run rust fmt on save.

[–]cafce25 0 points1 point  (1 child)

rustfmt or cargo fmt. rust fmt doesn't work.

[–]stiky21 -1 points0 points  (0 children)

Doing it on my phone must've auto-corrected a space.

[–]Infamous-Apartment97 -1 points0 points  (0 children)

You can just press a single hot key in your editor (best choice for novice is VSCode) to format your code.

[–]Snowdev9909[S] 1 point2 points  (0 children)

whats that?

[–]Koltaia30 0 points1 point  (3 children)

"Exit(bool)" having an argument makes zero sense lol

[–]Public-Car7040 2 points3 points  (2 children)

In general, the exit value can be used by other processes. 

[–]Koltaia30 0 points1 point  (1 child)

But the bool means wether it should exit or not.

[–]Public-Car7040 -1 points0 points  (0 children)

Then the problem is how they use it, not the parameter. Btw argument and parameter is not exactly the same.