Help to beautify the ugly code

⚓ Rust    📅 2026-01-19    👤 surdeus    👁️ 6      

surdeus

I have a code like below

for line in String::from_utf8_lossy(&output.stdout).lines() {
                        if let Some((key,val)) = line.split_once('=') {
                            key.strip_prefix("alias ").and_then(|alias| {
                                let mut vals = val.chars();
                                if val.len() > 2 && vals.next().unwrap() == '\'' && vals.last().unwrap() == '\'' {
                                    aliases.insert(alias.to_string(), val[1..val.len() - 1].split_whitespace().map(str::to_string).collect());
                                }
                                None::<()>
                            }).or_else(|| unsafe { env::set_var(key,val); None::<_>} );
                            
                        }
                    }

Specifically, I do not like returning None, it looks weird. However using a simple if makes the code even worse. Another thing I do not like - it's the way of trimming quotes. Any other code improvement suggestions are always welcome.

9 posts - 4 participants

Read full topic

🏷️ Rust_feed