If I define a function:
fn f() -> Result<(), E> {
// How to return Ok()?
}
How can I return the Ok
in std::result
with the unit type ()
?
It is an enum with the variants, Ok(T) , representing success and containing a value, and Err(E) , representing error and containing an error value. Functions return Result whenever errors are expected and recoverable.
However main is also able to have a return type of Result . If an error occurs within the main function it will return an error code and print a debug representation of the error (using the Debug trait).
The () type, also called “unit”. The () type has exactly one value () , and is used when there is no other meaningful value that could be returned. () is most commonly seen implicitly: functions without a -> ...
Enum std::result::Result1.0. #[must_use] pub enum Result<T, E> { Ok(T), Err(E), } Result is a type that represents either success ( Ok ) or failure ( Err ). See the std::result module documentation for details.
The only value of type ()
is ()
, so just put that inside the Ok
constructor:
fn f() -> Result<(), E> {
Ok(())
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With