【问题标题】:Paginate the UnorderedSet in near_sdk_rust在 near_sdk_rust 中对 UnorderedSet 进行分页
【发布时间】:2021-03-27 05:41:53
【问题描述】:

我要对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");
                }
            }
        }

【问题讨论】:

    标签: nearprotocol


    【解决方案1】:

    UnorderedSet 具有 iter() method,因此您可以使用标准的 Rust 迭代器方法,例如 skiptake

    products_set.iter().skip(start).take(end).collect()
    

    【讨论】:

      猜你喜欢
      • 2011-03-24
      • 2011-10-02
      • 2011-01-23
      • 2018-06-13
      • 1970-01-01
      • 1970-01-01
      • 2017-01-14
      • 1970-01-01
      相关资源
      最近更新 更多