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

10.8k Views Asked by At

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

2

There are 2 best solutions below

0
On BEST ANSWER

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

fn f() -> Result<(), E> {
    Ok(())
}
0
On

Use Ok(()) as in

fn f() -> Result<(), E> {
    Ok(())
}