我在向量中分组项目时遇到了问题。到目前为止,我所拥有的是以下内容(简化):
rs #[derive(Debug, Clone, Queryable, Serialize, Deserialize)]
pub(crate) struct User {
id: i64,
firstname: String,
lastname: String
}
#[derive(Debug, Clone, Queryable, Serialize, Deserialize)]
pub(crate) struct Item {
id: i64,
name: String,
owner: User,
owner_id: i64
}
let mut _items: Vec<Item> = items::dsl::items
.inner_join(users::table.on(users::id.eq(items::owner_id))
.select((
items::id,
items::name,
(
users::id
users::firstname,
users::lastname,
),
items::owner_id,
))
.order_by(items::owner_id)
.load::<Item>(&mut conn)?;
这个方法有效,我得到了一个项目向量.
现在我想要的是一个向量,其元素的形式为:
#[derive(Debug, Clone, Queryable, Serialize, Deserialize)]
pub(crate) struct UserWithItems {
id: i64,
firstname: String,
lastname: String,
items: Vec<SmallerItem>
}
where
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct SmallerItem {
id: i64,
name: String,
owner_id: i64
}
即每个条目对应于用户的向量,并且这些用户中的每一个具有包含其所有项的向量。
我尝试跟随https://stackoverflow.com/a/69663425/19389450:
let res: Vec<UserWithItems> = _items
.iter()
.group_by(|x| (x.user_id, x.firstname, x.lastname))
.into_iter()
.map(|((id, firstname, lastname), group)| UserWithItems {
id,
firstname,
lastname,
items: Some(group.map(|i| SmallerItem {
id: i.id,
name: i.name,
owner_id: i.owner_id
}).collect()),
})
.collect();
但我得到的错误:
|
126 | .group_by(|x| (x.user_id, x.firstname, x.lastname)
| ^^^^^^^^ method cannot be called on std::slice::Iter<'_, Item> due to
| unsatisfied trait bounds
|
::: /----/.rustup/toolchains/stable-aarch64-apple-
| darwin/lib/rustlib/src/rust/library/core/src/slice/iter.rs:66:1
|
66 | pub struct Iter<'a, T: 'a> {
| --------------------------
| |
| doesn't satisfy `std::slice::Iter<'_, Item>: Table`
| doesn't satisfy `std::slice::Iter<'_, Item>: diesel::QueryDsl`
|
= note: the following trait bounds were not satisfied:
`std::slice::Iter<'_, Item>: Table`
which is required by `std::slice::Iter<'_, Item>: diesel::QueryDsl`
`&std::slice::Iter<'_, Item>: Table`
which is required by `&std::slice::Iter<'_, Item>: diesel::QueryDsl`
`&mut std::slice::Iter<'_, Item>: Table`
which is required by `&mut std::slice::Iter<'_, Item>: diesel::QueryDsl`
[package]
name = "items"
version = "0.1.0"
authors = ["email"]
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
actix = "0.13.0"
actix-web = "4.2.1"
chrono = { version = "0.4.22", features = ["serde"]}
diesel = { version = "2.0.2", features = ["r2d2", "mysql", "chrono"] }
diesel_migrations = "2.0.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
dotenvy = "0.15.6"
env_logger = "0.10.0"
jwt = "0.16.0"
sha2 = "0.10.6"
hmac = "0.12.1"
derive_more = "0.99.17"
argon2 = "0.4.1"`your text`
rand_core = { version = "0.6", features = ["std"] }
futures-util = "0.3.25"
validator = { version = "0.16", features = ["derive"] }
thiserror = "1.0.37"
redis = { version = "0.22.1", features = ["r2d2"] }
1条答案
按热度按时间vof42yt11#
我设法用HashMap“解决”了它,如下所示。
不完全是我想要的,但现在很有效。