Hacker News new | ask | show | jobs
by Pxtl 3331 days ago
Not a Rust guy, but it looks like a way to say "this option value is required and if it is not present, crash with the following error". So "expect" is a fair name, since it means a value is expected and the absence of a value is unexpected.

I could see "required" or "require" as a better name. Or even just break the "positive names" rule and go with "notOptional".

1 comments

Looking at it that way, I think it's possible to make "expect" more comfortable to read by how you phrase your error messages.

Instead of

    let mut file = File::open("conf.json")
        .expect("could not open file");
and

    let config: Value = serde::from_reader(file)
        .expect("config has invalid json");
and

    let jenkins_server = config.get("jenkins_server")
        .expect("jenkins_server key not in config")
        .as_str()
        .expect("jenkins_server key is not a string");
One could write

    let mut file = File::open("conf.json")
        .expect("Need to be able to open file `conf.json'.");
and

    let config: Value = serde::from_reader(file)
        .expect("The file `conf.json' must contain valid JSON.");
and

    let jenkins_server = config.get("jenkins_server")
        .expect("The config must have a key named `jenkins_server'.")
        .as_str()
        .expect("The config value of `jenkins_server' must be a string.");
Something like that.
That is a good point. I'm going to have to start being more positive in my expectations :)