Maps (aka Associative Arrays)

This page is under construction. Please come back later.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
use std::collections::HashMap;
fn main() {
let mut dictionary:HashMap<String, String> = HashMap::new();
println!("dictionary size: {}", dictionary.len());
println!("dictionary is {}", (if dictionary.is_empty() { "empty" } else { "not empty" }));
dictionary.insert("apple", "red");
dictionary.insert("banana", "yellow");
dictionary.insert("cantaloupe", "orange");
dictionary.insert("dragonfruit", "red");
dictionary.insert("elderberry", "purple");
println!("dictionary size: {}", dictionary.len());
println!("dictionary is {}", (if dictionary.is_empty() { "empty" } else { "not empty" }));
println!("bananas are {}", dictionary.get("banana").unwrap());
let mut x:&'static str = "dragonfruit";
if dictionary.contains_key(x) {
println!("{} are {}", x, dictionary.get(x).unwrap());
} else {
println!("Can't find {}", x);
}
x = "fig";
if dictionary.contains_key(x) {
println!("{} are {}", x, dictionary.get(x).unwrap());
} else {
println!("Can't find {}", x);
}
x = "elderberry";
dictionary.remove(x);
if dictionary.contains_key(x) {
println!("{} are {}", x, dictionary.get(x).unwrap());
} else {
println!("Can't find {}", x);
}
println!("{}", map_to_string!(dictionary));
}
Output
$ rustc Maps1.rs
error[E0425]: cannot find value `Utils` in this scope
--> Maps1.rs:41:17
|
41 | println!("{}", Utils.mapToString(dictionary));
| ^^^^^ not found in this scope
error[E0425]: cannot find function `isEmptyMap` in this scope
--> Maps1.rs:7:35
|
7 | println!("dictionary is {}", (if isEmptyMap(dictionary) { "empty" } else { "not empty" }));
| ^^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `isEmptyMap` in this scope
--> Maps1.rs:15:35
|
15 | println!("dictionary is {}", (if isEmptyMap(dictionary) { "empty" } else { "not empty" }));
| ^^^^^^^^^^ not found in this scope
error[E0599]: no method named `at` found for struct `HashMap` in the current scope
--> Maps1.rs:17:40
|
17 | println!("bananas are {}", dictionary.at("banana"));
| ^^ method not found in `HashMap<String, String>`
error[E0425]: cannot find function `mapContains` in this scope
--> Maps1.rs:20:5
|
20 | if mapContains(dictionary,x) {
| ^^^^^^^^^^^ not found in this scope
error[E0599]: no method named `at` found for struct `HashMap` in the current scope
--> Maps1.rs:21:39
|
21 | println!("{} are {}", x, dictionary.at(x));
| ^^ method not found in `HashMap<String, String>`
error[E0425]: cannot find function `mapContains` in this scope
--> Maps1.rs:27:5
|
27 | if mapContains(dictionary,x) {
| ^^^^^^^^^^^ not found in this scope
error[E0599]: no method named `at` found for struct `HashMap` in the current scope
--> Maps1.rs:28:39
|
28 | println!("{} are {}", x, dictionary.at(x));
| ^^ method not found in `HashMap<String, String>`
error[E0425]: cannot find function `mapRemove` in this scope
--> Maps1.rs:34:2
|
34 | mapRemove(dictionary,x);
| ^^^^^^^^^ not found in this scope
error[E0425]: cannot find function `mapContains` in this scope
--> Maps1.rs:35:5
|
35 | if mapContains(dictionary,x) {
| ^^^^^^^^^^^ not found in this scope
error[E0599]: no method named `at` found for struct `HashMap` in the current scope
--> Maps1.rs:36:39
|
36 | println!("{} are {}", x, dictionary.at(x));
| ^^ method not found in `HashMap<String, String>`
error: aborting due to 11 previous errors
Some errors have detailed explanations: E0425, E0599.
For more information about an error, try `rustc --explain E0425`.
#![allow(dead_code)]
#![allow(non_snake_case)]
#![allow(non_upper_case_globals)]
#[macro_use]
mod utils;
use std::collections::HashMap;
fn main() {
let mut planet_diameters:HashMap<String, isize> = [
("Mercury", 4879),
("Venus", 12103),
("Earth", 12756),
("Mars", 6794),
("Jupiter", 142985),
("Saturn", 120534),
("Uranus", 51115),
("Neptune", 49534),
("Pluto", 2374),
("Ceres", 946),
("Eris", 2326),
("Makemake", 1430)
].into();
for planet in planet_diameters.keys().cloned() {
println!("{} has a diameter of {} km", planet, planet_diameters.get(planet).unwrap());
}
}
Output
$ rustc Maps2.rs
error: this is a block expression, not an array
--> Maps2.rs:6:3
|
6 | {"Mercury", 4879},
| ^^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
6 | ["Mercury", 4879],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:7:3
|
7 | {"Venus", 12103},
| ^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
7 | ["Venus", 12103],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:8:3
|
8 | {"Earth", 12756},
| ^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
8 | ["Earth", 12756],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:9:3
|
9 | {"Mars", 6794},
| ^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
9 | ["Mars", 6794],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:10:3
|
10 | {"Jupiter", 142985},
| ^^^^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
10 | ["Jupiter", 142985],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:11:3
|
11 | {"Saturn", 120534},
| ^^^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
11 | ["Saturn", 120534],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:12:3
|
12 | {"Uranus", 51115},
| ^^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
12 | ["Uranus", 51115],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:13:3
|
13 | {"Neptune", 49534},
| ^^^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
13 | ["Neptune", 49534],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:14:3
|
14 | {"Pluto", 2374},
| ^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
14 | ["Pluto", 2374],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:15:3
|
15 | {"Ceres", 946},
| ^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
15 | ["Ceres", 946],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:16:3
|
16 | {"Eris", 2326},
| ^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
16 | ["Eris", 2326],
| ~ ~
error: this is a block expression, not an array
--> Maps2.rs:17:3
|
17 | {"Makemake", 1430}
| ^^^^^^^^^^^^^^^^^^
|
help: to make an array, use square brackets instead of curly braces
|
17 | ["Makemake", 1430]
| ~ ~
error[E0433]: failed to resolve: use of undeclared type `Utils`
--> Maps2.rs:20:16
|
20 | for planet in Utils::keys(planetDiameters) {
| ^^^^^ use of undeclared type `Utils`
error: aborting due to 13 previous errors
For more information about this error, try `rustc --explain E0433`.
Questions
- {{Who's on first?}}
- {{Who's on second?}}
- {{Who's on third?}}
Projects
More ★'s indicate higher difficulty level.
References
- [[Rust Language Reference]]
- [[Rust Compiler]]
Pure Programmer


