Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Avoid constructing string just to match

I just wrote this, which feels ugly in a low level language:

    match std::env::var("IN_DEPLOYMENT")
        .unwrap_or("false".to_string())
        .as_str()
    {
        "false" | "FALSE" | "0" => {
            tracing_subscriber::fmt().init();
        }
        _ => tracing_subscriber::fmt().json().init(),
    }

How do I avoid needing the temporary string? My first thought was to match directly on the result, but I couldn’t work out how to tranform Result<String,_> into Result<&str, _>.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You should firstly convert Result<String, E> to Result<&str, &E>. This can be done with Result::as_deref method:

fn foo() {
    match std::env::var("IN_DEPLOYMENT").as_deref().unwrap_or("false") {
        "false" | "FALSE" | "0" => todo!(),
        _ => todo!(),
    }
}

On the other note if you would really need an owned String, prefer Result::unwrap_or_else over Result::unwrap_or (same argument goes for Option), because this will prevent allocation when there is data to unwrap. There is clippy lint that checks for it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading