在 near_sdk_rust 中对 UnorderedSet 进行分页
Paginate the UnorderedSet in near_sdk_rust
我想对 UnorderedSet 进行分页,可以吗,我必须使用什么集合?
这是我的代码:
user_products_map: TreeMap<u128, UnorderedSet<u128>>
pub fn get_products_of_user_id(&self, user_id: u128) -> Vec<u128> {
let products_set_option = self.user_products_map.get(&user_id);
match products_set_option {
Some(products_set) => products_set.to_vec(),
None => {
panic!("No products for user");
}
}
}
我想像这样分页:
pub fn get_products_of_user_id(&self, start:u128, end:u128, user_id: u128) -> Vec<u128> {
let products_set_option = self.user_products_map.get(&user_id);
match products_set_option {
Some(products_set) => products_set[start, end].to_vec(),
None => {
panic!("No products for user");
}
}
}
UnorderedSet
有 iter()
method, so you can use standard Rust Iterator methods like skip
and take
products_set.iter().skip(start).take(end).collect()
我想对 UnorderedSet 进行分页,可以吗,我必须使用什么集合?
这是我的代码:
user_products_map: TreeMap<u128, UnorderedSet<u128>>
pub fn get_products_of_user_id(&self, user_id: u128) -> Vec<u128> {
let products_set_option = self.user_products_map.get(&user_id);
match products_set_option {
Some(products_set) => products_set.to_vec(),
None => {
panic!("No products for user");
}
}
}
我想像这样分页:
pub fn get_products_of_user_id(&self, start:u128, end:u128, user_id: u128) -> Vec<u128> {
let products_set_option = self.user_products_map.get(&user_id);
match products_set_option {
Some(products_set) => products_set[start, end].to_vec(),
None => {
panic!("No products for user");
}
}
}
UnorderedSet
有 iter()
method, so you can use standard Rust Iterator methods like skip
and take
products_set.iter().skip(start).take(end).collect()