Is there a way to display unnamed arguments?


Okay, this question sounds a bit crazy. Hopefully you can at least understand why I am asking. :wink:

Even in the first example in the Rust book we come across trying to parse a number from a string like so:

    let guess: u32 = guess.trim().parse()
        .expect("Please type a number!");

And I often find myself doing similar things. However, when this fails the error doesn’t really tell me anything about the problem. I can go back in to add debugging but I will at least have to do something like:

    let word = guess.trim();
    let guess: u32 = word.parse()
        .expect(&format!("Failed to parse \"{}\" as an integer.", word));

Note that I had to introduce an extra variable to capture the input of the call that returns the Option or Result. I was wondering if there is any shorthand for getting a slightly more descriptive error message, possibly showing the input, without manually writing it out.


No, you need to make a variable instead of the temporary to get such extra information.

Also if your program is targeted at users then you should aim to never panic. (With panic then usually indicating a bug in the program.)

More elaborate software (Localised inparticular) will generally split processing from output, in such cases you will have a function returning a custom error type which likely includes that extra info. using something like;

let word = guess.trim();
let guess: u32 = word.parse().map_err(|e| MyError::from((e, word)))?;