What does an empty set of parentheses mean when used in a generic type declaration?

后端 未结 1 487
深忆病人
深忆病人 2020-12-10 00:57

The Display trait is defined as follows:

pub trait Display {
    fn fmt(&self, &mut Formatter) -> Result<(), Error>;
}
<         


        
相关标签:
1条回答
  • 2020-12-10 01:10

    () is an empty tuple, a simple zero-sized type (it uses no memory) with only one value possible, (). It’s also known as the unit type. Its use in a return type of Result<(), E> means “if nothing goes wrong, there’s no further value produced”. The semantics are what’s important—the call was OK.

    Result<(), ()> would also make sense as a return type—either something succeeded, or it failed, with nothing more to report in either case.

    0 讨论(0)
提交回复
热议问题