Robert
Robert

Reputation: 161

Variable assignment based on condition

I try to assign a PathBuf result to variable based on a condition:

// USE_OLD_DATA set at compilation time to FALSE or TRUE
let root = env::current_dir().unwrap();

let processed_files_location = if USE_OLD_DATA {
    root.join("Result Data 10-28-2020");
} else {
    root.join("Result Data");
};

after this ... the processed_files_location = () ?!

Upvotes: 0

Views: 80

Answers (1)

Maximilian Burszley
Maximilian Burszley

Reputation: 19704

Since conditionals are expressions, you can just pass the expression to your join method:

let processed_files_location =
    root.join(
        if USE_OLD_DATA {
            "Result Data 10-28-2020"
        } else {
            "Result Data"
        }
    );

And, as pointed out in the comments, when you terminate your branch expressions with ;, it evaluates to the unit tuple () rather than the type you're expecting.

Upvotes: 3

Related Questions