Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return Ok unit type of std::result<(), E>?

Tags:

rust

unit-type

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 ()?

like image 347
WiSaGaN Avatar asked Jan 14 '15 14:01

WiSaGaN


People also ask

What does OK () do in Rust?

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.

Can main return a result rust?

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).

What is unit type in Rust?

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 -> ...

What is enum result in rust?

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.


1 Answers

The only value of type () is (), so just put that inside the Ok constructor:

fn f() -> Result<(), E> {
    Ok(())
}
like image 94
DK. Avatar answered Sep 30 '22 02:09

DK.