为什么Cow的定义在Owned(<b as =“” toowned =“”> :: Owned)中使用“ as”?

时间:2018-08-26 16:28:14

标签: rust

Cow的定义方式如下:

pub enum Cow<'a, B>
where
    B: 'a + ToOwned + ?Sized,
{
    Borrowed(&'a B),
    Owned(<B as ToOwned>::Owned),
}

为什么B as ToOwned在那儿?为了删除'a?Sized的界限?

1 个答案:

答案 0 :(得分:4)

它只是通过fully qualified syntax被显式显示。同样可以将其定义为

pub enum Cow<'a, B>
where
    B: 'a + ToOwned + ?Sized,
{
    Borrowed(&'a B),
    Owned(B::Owned),
}

此语法是您访问B的{​​{1}}实现的关联类型的方式。