我认为这个问题最好用一个例子来描述:
enum SensitiveVector<T> {
Empty,
Filled(Vec<T>),
}
impl<T> for SensitiveVector<T> {
pub fn append(mut self, other: Self) -> Self {
match (&self, &other) {
(Empty, Empty) => Empty,
(Empty, Filled(_)) => other,
(Filled(_), Empty) => self,
(mut vector @ Filled(_), Filled(other_items) => {
vector.0.append(other_items);
vector
}
}
}
}
我试图提高效率,而不是简单地克隆向量或类似的,但重用内存(希望)有效。无论如何,比赛表达式的第四个手臂是我的问题。我只是不知道如何将other
示例的向量项附加到self
,如果这个手臂匹配的话。我尝试应用我在match arm中看到的任何语法,但它总是告诉我类型只是&SensitiveVector<T>
,所以访问元组字段0
是不可能的。
我知道代码可能很可怕。但是谁能告诉我怎么做这样的事情?当然,我可以只使用这两个向量并创建一个新的SensitiveVector
示例,但我认为扩展self
更有效。
感谢您的任何帮助,并解释我的语法/概念/规则,我错过了。
2条答案
按热度按时间qhhrdooz1#
在值和引用之间存在一些不匹配。既然你似乎想要处理值(使用参数来发出新值),我建议你一直使用值(特别是删除match语句中的引用)。
一旦match语句破坏了
self
或other
,这两个值就不再存在了,只是破坏后的成员仍然存在,那么你必须从这些成员重新创建一个枚举(而不是试图重用整个self
或other
)。这是你的例子,有几个修正。
fkvaft9z2#
问题是双重的:
1.你在
(&self, &other)
上匹配,所以模式中的值总是 * 共享引用 *,它们不能被拥有(返回),也不能被修改。1.枚举变体不是rust中的类型,并且rust没有类型改进。所以
vector @ Filled(_)
将给予一个vector: SensitiveVector
(在这里,它最多是一个&SensitiveVector
),你不能从枚举本身访问枚举变量的字段,所以如果你的目标是更新内容,那么模式匹配在很大程度上是浪费的。因为你无论如何都要通过值来获取
self
和other
,所以第一个修复方法是也匹配它们,这意味着你还需要返回你在第二和第三个臂中匹配的值,或者需要从内容中重构值:这并没有修复第四个手臂,但是现在你不应该使用@-模式,你应该把内部的向量移出来,合并向量,然后重新 Package 结果: