Skip to content

Commit 2084903

Browse files
refactor: dot notation
Use dot notation to shorten lines and add conditionals for iterators upon matches
1 parent 9b46818 commit 2084903

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

src/main.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,20 @@ fn main() {
1212
// pairs into a vector
1313
let dotenv_vars: Vec<_> = dotenvy::dotenv_iter().unwrap().map(|x| x.unwrap()).collect();
1414

15+
1516
// Print env vars to stdout
1617
match dotenv() {
1718
Ok(_) => {
18-
// If .env file loaded successfully, greet the user
19-
if let Some((_, value)) = env_vars.iter().find(|(key, _)| key == "NAME") {
20-
println!("Hello, {}!\n", value);
21-
} else {
22-
println!("Hello, world!\n");
23-
}
19+
// If env var is found, greet the user
20+
let name = env_vars
21+
.iter()
22+
.chain(dotenv_vars.iter())
23+
.find(|(key, _)| key == "NAME")
24+
.map(|(_, value)| value.to_string())
25+
.unwrap_or("world".to_string());
26+
27+
println!("Hello, {}!\n", name);
28+
2429
// read env vars from env::vars()
2530
println!("env vars:");
2631
for (key, value) in &env_vars {
@@ -29,6 +34,7 @@ fn main() {
2934
}
3035
}
3136
println!();
37+
3238
// read env vars from dotenvy::dotenv_iter()
3339
println!("dotenv vars:");
3440
for (key, value) in &dotenv_vars {

0 commit comments

Comments
 (0)