问号在类型参数绑定中意味着什么? [英] What does the question mark mean in a type parameter bound?

查看:70
本文介绍了问号在类型参数绑定中意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  pub特质借用特征<借用>:借用<借> 
其中
借入:?尺码,
{
fn borrow_mut(& mut self) - > & mut借用;

$ / code>

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 (maybe Sized )。



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:

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

这篇关于问号在类型参数绑定中意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆