rustHow to create a HashMap of pointers in Rust?
A HashMap of pointers in Rust can be created using the HashMap
type from the std::collections
module.
use std::collections::HashMap;
let mut map: HashMap<&str, *const i32> = HashMap::new();
let a = 10;
let b = 20;
map.insert("a", &a);
map.insert("b", &b);
The code above creates a HashMap
with &str
keys and *const i32
values. The &a
and &b
are references to the i32
variables a
and b
.
use std::collections::HashMap
: imports theHashMap
type from thestd::collections
module.let mut map: HashMap<&str, *const i32> = HashMap::new();
: creates aHashMap
with&str
keys and*const i32
values.map.insert("a", &a);
: inserts a key-value pair into theHashMap
, with the key being"a"
and the value being a pointer to thei32
variablea
.map.insert("b", &b);
: inserts a key-value pair into theHashMap
, with the key being"b"
and the value being a pointer to thei32
variableb
.
Helpful links
Related
- How to implement PartialEq for a Rust HashMap?
- How to use a tuple as a key in a Rust HashMap?
- How to convert a Rust HashMap to JSON?
- How to convert the keys of a Rust HashMap to a vector?
- How to convert a Rust HashMap to a JSON string?
- How to use a HashBrown with a Rust HashMap?
- How to sort the keys in a Rust HashMap?
- How to use a custom hash function with a Rust HashMap?
- How to remove an element from a Rust HashMap if a condition is met?
- How to create a Rust HashMap with a string key?
More of Rust
- How to iterate hashset in Rust
- How to get a capture group using Rust regex?
- How to use regex with bytes in Rust?
- How to replace a capture group using Rust regex?
- How to match the end of a line in a Rust regex?
- How to compile a regex in Rust?
- How to use regex to match a double quote in Rust?
- How to replace all using regex in Rust?
- How to use regex to match a group in Rust?
- How to calculate the inverse of a matrix in Rust?
See more codes...