我正在尝试创建一个引用同一个表两次的结构。这样做的目的是创建一种类别层次结构。这是我正在尝试对下表执行的操作:
create table product_category_rollup(
id serial primary key,
upper_category_id integer not null,
lower_category_id integer not null,
foreign key (upper_category_id) references product_category(id),
foreign key (lower_category_id) references product_category(id)
);
create table product_category(
id serial primary key,
name varchar unique not null
);
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建匹配的结构,如下所示:
#[derive(Identifiable, Queryable)]
#[table_name = "product_category"]
pub struct ProductCategory {
id: i32,
name: String,
}
#[derive(Queryable, Identifiable, Associations)]
#[belongs_to(ProductCategory, foreign_key="upper_category_id")]
#[belongs_to(ProductCategory, foreign_key="lower_category_id")]
#[table_name = "product_category_rollup"]
pub struct ProductCategoryRollup {
id: i32,
upper_category_id: i32,
lower_category_id: i32,
}
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
error[E0119]: conflicting implementations …Run Code Online (Sandbox Code Playgroud) 我目前正在学习 Rust,我偶然发现了一个操作,我在 std 中找不到标准实现,也找不到合理形成的代码片段,这可以做我想做的事情。
基本上我想重复给定次数的迭代器的每个元素。因此,例如,如果 a 的迭代器为[1,2,3],那么例如通过将每个元素重复 3 次,我的意思是输出应该是[1,1,1,2,2,2,3,3,3].
如何在 Rust 中惯用地做到这一点?