在泛型类型声明中使用空括号是什么意思? [英] What does an empty set of parentheses mean when used in a generic type declaration?
问题描述
Display
特性定义如下:
pub trait Display {
fn fmt(&self, &mut Formatter) -> Result<(), Error>;
}
对我来说最神秘的是类型声明 Result<(), Error>
中的空括号集 ()
.它是什么以及它的目的是什么?
The most mysterious thing to me is the empty set of parentheses, ()
, in the type declaration Result<(), Error>
. What is it and its purpose?
推荐答案
()
是一个空元组,一个简单的零大小类型(它不使用内存),只有一个可能的值,<代码>()代码>.它也被称为单位类型.它在 Result<(), E>
的返回类型中的使用意味着如果没有出错,就不会产生更多的值".语义很重要——调用没问题.
()
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<(), ()>
作为返回类型也有意义——要么成功,要么失败,在任何一种情况下都没有更多的报告.
Result<(), ()>
would also make sense as a return type—either something succeeded, or it failed, with nothing more to report in either case.
这篇关于在泛型类型声明中使用空括号是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!