If the metadata
function returned a Result::Err
, presumably it wasn't able to create the Metadata
. Perhaps the file didn't exist, for example. That's the error condition that you now need to think about how to handle. Perhaps you error out of your program, or perhaps you return the error from this function, etc.
Using some default Metadata
instead is a possibility, but seems unlikely in this particular case. If that is what you need to do, though, you'll need some way to create the default metadata. In Rust, data structures with sensible defaults implement the Default
trait, which has an associated function called default
. You would use it something like this:
// Assuming `Metadata` implements `Default`
let mut meta = match metadata("Hello.txt") {
Ok(m) => m,
Err(e) => Metadata::default() ,
// Or `Default::default(),` would also work
};
Or more briefly and idiomatically,
let mut meta = metadata("Hello.txt").unwrap_or_default();
// Alternative forms I'll not explain in detail here:
// let mut meta = metadata("Hello.txt").unwrap_or_else(|_| Default::default);
// let mut meta = metadata("Hello.txt").unwrap_or(Default::default());
Note that unlike some other languages, there is nothing special about any new
functions or methods that happen to exist. The closest thing to a default constructor is implementing the standard Default
trait, and it's not a requirement (not all data structures implement the trait).
If it's more appropriate to return an error, your function will return a Result
, and you'll usually want something like this:
// Note the `?`
let mut meta = metadata("Hello.txt")?;
Which is short hand for something close to this:
let mut meta = match metadata("Hello.txt") {
Ok(m) => m,
Err(e) => return Err(e),
// The error case is in reality little more complicated as it can
// coerce the given error type into a different error type sometimes,
// but again I'll skip the details for now
};
As you can see, the error arm in the match
statement need not return a Metadata
to be assigned to meta
. Instead it can return from the function entirely, or it could panic and end the program, etc. Without more context, I'd guess that this is the more appropriate approach for your use case.