HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RustVariablesData TypesOperatorsStringsVectors (Vec)Conditions (if / match)Loops (for / while)FunctionsOwnership & Borrowing
REFERENCE
📖 Overview
🖥️ Output
📦 Variables
🔢 Numbers & Math
✍️ String
📋 Vector & HashMap
Veciter / map / sumHashMap
🔁 Condition & Loop
🧩 Function
🔑 Ownership & Borrowing
Rust›REFERENCE›Vector & HashMap
📋

Vector & HashMap

Flexible lists (Vec) and key-value pairs (HashMap).

CommandDescription
VecFlexible (growable) list.
iter / map / sumTransformation and collection using an iterator.
HashMapKey-value mapping.

Vec

Try it yourself →

Flexible (growable) list.

Syntax

let mut v = vec![...];

Description

push adds items (requires mut), len() gives the size, v[i] accesses items. Printed with {:?}.

Example

fn main() {
    let mut v = vec![1, 2];
    v.push(3);
    println!("{:?} {}", v, v.len());
}

Expected output

[1, 2, 3] 3

When to use?

Storing lists.

Common mistakes

  • ⚠️ push requires the variable to be mutable: let mut v.

iter / map / sum

Try it yourself →

Transformation and collection using an iterator.

Syntax

v.iter().map(|x| ...).sum()

Description

iter() iterates over elements; map transforms, filter filters, sum totals. Returns a Vec via collect.

Example

fn main() {
    let v = vec![1, 2, 3, 4];
    let total: i32 = v.iter().sum();
    println!("{}", total);
}

Expected output

10

When to use?

Summation, transformation, filtering.

HashMap

Try it yourself →

Key-value mapping.

Syntax

use std::collections::HashMap;

Description

insert adds an item, get(&key) returns an Option. Imported with use.

Example

use std::collections::HashMap;
fn main() {
    let mut m = HashMap::new();
    m.insert("age", 36);
    println!("{}", m["age"]);
}

Expected output

36

When to use?

Dictionary, counter.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code