关于泛型:问号在类型参数绑定中是什么意思?

What does the question mark mean in a type parameter bound?

我找到了std::borrow::BorrowMut的定义:

1
2
3
4
5
6
pub trait BorrowMut<Borrowed>: Borrow<Borrowed>
where
    Borrowed: ?Sized,
{
    fn borrow_mut(&mut self) -> &mut Borrowed;
}

Sized前面的问号在此类型参数绑定(Borrowed: ?Sized)中意味着什么?

我咨询了:

  • The Rust Programming Language?1书,
  • The Rust Reference?2,还有
  • 什么是"未实现大小调整"?意思?在堆栈溢出

上,但没有找到解释。请在回答中提供参考。

?1 尤其是请参阅第5.20节特征?2 和第6.1.9节特征

<铅>


这意味着特征是可选的。当前语法是DST语法RFC中引入的。

我知道对?有效的唯一特征是Sized

在此特定示例中,我们可以实现BorrowMut用于未定尺寸的类型,例如[T] a€",请注意此处没有&

一个内置实现使用了该功能:

1
impl< T > BorrowMut<[T]> for Vec< T >

如Matthieu M 。添加:

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 generic T would be assumed to be Sized. The way to note the opposite would be to mark it ?Sized ("maybe Sized").