'Is it a good practice to always return a Result<T, E> from functions? [closed]

I realize that I always use Result when I need to return something from a function.

Is it something usual in a good Rust development? And should I also return a Result for a void function that might encounter an error (like Result<(), Error>)?



Solution 1:[1]

Yes. Returning Result is very common in Rust.

In practice, this can often be shortened to Result<T> instead of Result<T, E>. I'm not saying that's always a good idea, but in many cases it is and can save some typing. Here is an example with the anyhow's Result type:

pub type Result<T, E = Error> = core::result::Result<T, E>;

If you have a "void" function that could return an error, you can declare the function as returning Result<()>.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 at54321