Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Find Rust Guru to Help With Code Review #3

Open
Miserlou opened this issue May 18, 2018 · 1 comment
Open

Find Rust Guru to Help With Code Review #3

Miserlou opened this issue May 18, 2018 · 1 comment
Labels
help wanted Extra attention is needed

Comments

@Miserlou
Copy link
Owner

I'm new to Rust. I feel like I'm using mut in places I shouldn't. I need Rust pro to help me with code review.

@Miserlou Miserlou added the help wanted Extra attention is needed label May 18, 2018
@Fierthraix
Copy link
Contributor

Fierthraix commented Jun 29, 2018

When you have this code at line 68:

// --until-contains
let mut has_matched = false;
let mut has_until_contains = false;
let mut until_contains = "";
if matches.is_present("until_contains"){
    has_until_contains = true;
    until_contains = matches.value_of("until_contains").unwrap();
}

which is used on line 177 as

if has_until_contains {
    if line.contains(until_contains){
        has_matched=true;
    }
}

it is more idiomatic to have until_contains be Option<String>, which implicitly encodes the has_until_contains = false case as None.

As clap already does that you can delete all of the above code and replace it with this at line 177:

if let Some(until_contains) = matches.value_of("until_contains") {
    if line.contains(until_contains) {
        has_matched = true;
    }
}

I feel as though you added all that code outside the loop for fear that the lookup in matches is performed every loop iteration. Because matches isn't mut the compiler should only evaluate it once.

To get around this I recommend the structopt crate. This lets all of the variables you have be fields on a struct that are evaluated when the command first runs, thus avoiding the possibility that things like the regex in in until_match get evaluated every loop iteration.

I am currently creating a branch that does all this. It should simplify the code greatly.

I would also recommend using clippy and rustfmt on your code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants