问号在类型参数绑定中意味着什么? [英] What does the question mark mean in a type parameter bound?
问题描述
我找到 std的定义: :borrow :: BorrowMut
:
pub特质借用特征<借用>:借用<借>
其中
借入:?尺码,
{
fn borrow_mut(& mut self) - > & mut借用;
$ / code>
Sized
在此类型参数中绑定的意义(借用:?大小
)?
- 生锈的程式语言¹书,
- Rust Reference ,还有
- 什么是Sized不是实现"意思是?堆栈溢出
但没有找到解释。请在答案中提供参考。
¹特别参见 5.20特质
²和section 6.1.9 Traits 这意味着特质是可选的。当前的语法是在 DST语法RFC 。
我知道的?
的唯一特征是大小
。
在这个特定的例子中,我们可以为实现 BorrowMut
>未分类的类型,例如 [T]
- 注意这里没有&
!
$ b pre $ impl< T> BorrowMut< [T]>对于Vec< T>
由于 Matthieu M.添加:
这是一个扩展边界的情况;在一般范围内施加约束,但在
Sized
的情况下,决定除非另有说明,通用的T
将假定为大小
。注意相反的方法是将它标记为?Sized
(maybeSized
)。
I found the definition for std::borrow::BorrowMut
:
pub trait BorrowMut<Borrowed>: Borrow<Borrowed>
where
Borrowed: ?Sized,
{
fn borrow_mut(&mut self) -> &mut Borrowed;
}
What does the question mark in front of Sized
mean in this type parameter bound (Borrowed: ?Sized
)?
I consulted:
- The Rust Programming Language¹ book,
- The Rust Reference², and also
- What does "Sized is not implemented" mean? on Stack Overflow
but didn't find an explanation. Please give a reference in your answer.
¹ especially see section 5.20 Traits
² and section 6.1.9 Traits
It means that the trait is optional. The current syntax was introduced in the DST syntax RFC.
The only trait I am aware of that works for ?
is Sized
.
In this specific example, we can implement BorrowMut
for unsized types, like [T]
— note that there's no &
here!
One built-in implementation makes use of that:
impl<T> BorrowMut<[T]> for Vec<T>
As Matthieu M. adds:
This is a case of a widening bound; in general bounds impose more constraints, but in the case of
Sized
it was decided that unless otherwise noted a genericT
would be assumed to beSized
. The way to note the opposite would be to mark it?Sized
("maybeSized
").
这篇关于问号在类型参数绑定中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!