在泛型类型声明中使用空括号是什么意思?

编程入门 行业动态 更新时间:2024-10-26 22:27:42
本文介绍了在泛型类型声明中使用空括号是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Display 特性定义如下:

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

对我来说最神秘的是类型声明 Result 中的空括号集 ().它是什么以及它的目的是什么?

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 的返回类型中的使用意味着如果没有出错,就不会产生更多的值".语义很重要——调用没问题.

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

更多推荐

在泛型类型声明中使用空括号是什么意思?

本文发布于:2023-11-09 10:33:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572152.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:括号   声明   类型

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!