Pure Programmer
Blue Matrix


Cluster Map

Maps (aka Associative Arrays)

L1

This page is under construction. Please come back later.

Maps1.rs
use std::collections::HashMap;

fn main() {
	let mut dictionary:HashMap<String, String> = HashMap::new();

	println!("dictionary size: {}", dictionary.len());
	println!("dictionary is {}", (if isEmptyMap(dictionary) { "empty" } else { "not empty" }));
	dictionary["apple"] = String::from("red");
	dictionary["banana"] = String::from("yellow");
	dictionary["cantaloupe"] = String::from("orange");
	dictionary["dragonfruit"] = String::from("red");
	dictionary["elderberry"] = String::from("purple");

	println!("dictionary size: {}", dictionary.len());
	println!("dictionary is {}", (if isEmptyMap(dictionary) { "empty" } else { "not empty" }));

	println!("bananas are {}", dictionary.at("banana"));

	let mut x:&str = "dragonfruit";
	if mapContains(dictionary,x) {
		println!("{} are {}", x, dictionary.at(x));
	} else {
		println!("Can't find {}", x);
	}

	x = "fig";
	if mapContains(dictionary,x) {
		println!("{} are {}", x, dictionary.at(x));
	} else {
		println!("Can't find {}", x);
	}

	x = "elderberry";
	mapRemove(dictionary,x);
	if mapContains(dictionary,x) {
		println!("{} are {}", x, dictionary.at(x));
	} else {
		println!("Can't find {}", x);
	}

	println!("{}", Utils.mapToString(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`.
Maps2.rs
mod utils;
use std::collections::HashMap;

fn main() {
	let mut planetDiameters: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}
	];

	for planet in Utils::keys(planetDiameters) {
		println!("{} has a diameter of {} km", planet, planetDiameters.at(planet));
	}
}

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

Projects

More ★'s indicate higher difficulty level.

References