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

前端 未结 1 1103
旧巷少年郎
旧巷少年郎 2020-12-03 02:28

I found the definition for std::borrow::BorrowMut:

pub trait BorrowMut: Borrow
where
    Borrowed: ?Sized,
{
    fn borrow_mu         


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

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

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