• 【Rust】哈希映射(一)


    环境

    • Time 2022-03-24
    • Rust 1.59.0

    示例

    new

    fn main() {
        let map: HashMap<&str, i32> = HashMap::new();
        println!("{:?}", map.capacity());
    }
    

    with_capacity

    fn main() {
        let map: HashMap<&str, i32> = HashMap::with_capacity(4);
        println!("{:?}", map.capacity());
    }
    

    with_hasher

    fn main() {
        let s = RandomState::new();
        let map: HashMap<&str, i32> = HashMap::with_hasher(s);
        println!("{:?}", map.capacity());
    }
    

    with_capacity_and_hasher

    fn main() {
        let s = RandomState::new();
        let map: HashMap<&str, i32> = HashMap::with_capacity_and_hasher(4, s);
        println!("{:?}", map.capacity());
    }
    

    capacity

    fn main() {
        let map: HashMap<&str, i32> = HashMap::with_capacity(4);
        println!("{:?}", map.capacity());
    }
    

    keys

    fn main() {
        let map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
        map.keys().for_each(|e| println!("{e:?}"));
    }
    

    into_keys

    fn main() {
        let map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
        map.into_keys().for_each(|e| println!("{e:?}"));
    }
    

    values

    fn main() {
        let map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
        map.values().for_each(|e| println!("{e:?}"));
    }
    

    values_mut

    fn main() {
        let mut map = HashMap::from([("a", 1), ("b", 2), ("c", 3)]);
        map.values_mut().for_each(|e| println!("{e:?}"));
    }
    

    总结

    了解了哈希映射中相关的一些方法。

    附录

  • 相关阅读:
    如何选择开源许可证?(转)
    gdb的使用(转)
    git的使用
    2017-3-13 leetcode 4 11 15
    2017-3-12 leetcode 167 209 216
    2017-3-11 leetcode 217 219 228
    2017-3-10 leetcode 229 238 268
    1175: 零起点学算法82——find your present
    1174: 零起点学算法81——求整数绝对值
    1173: 零起点学算法80——求实数绝对值
  • 原文地址:https://www.cnblogs.com/jiangbo4444/p/16322811.html
Copyright © 2020-2023  润新知