【发布时间】:2016-12-18 07:27:43
【问题描述】:
我有这样的代码,我需要在两个单独的哈希图中插入一些东西。我只想要实现 Clone 的泛型类型。
use std::collections::HashMap;
use std::clone::Clone;
pub struct Something<A, B> {
hm1: HashMap<usize, B>,
hm2: HashMap<usize, B>,
other: A,
}
impl<A, B> Something<A, B>
where B: Clone
{
fn add_to_both_hm(&mut self, x: usize, y: usize, weight: B) {
self.hm1.insert(x, weight.Clone());
self.hm2.insert(y, weight);
}
}
但是在编译时,编译器会报错error: no method named 'Clone' found for type 'B' in the current scope。
为什么即使我指定了where B: Clone,它仍然会出错?我该如何解决?
【问题讨论】:
-
方法是
clone。 Rust 是一种区分大小写的语言。 编辑:我刚刚意识到您的问题可能是:Clone不是方法,而是特征,特征的名称与它定义的方法。 -
@DK。你是对的,“克隆”的拼写错误是这里的问题。你能把它写成答案,以便它可以被接受/赞成吗?
标签: rust