Skip to Content
Collections & Modules

Collections & Modules

Collections & iterators

Vec<T> and HashMap<K,V> are your MutableList/MutableMap. The iterator chains will feel very familiar:

Kotlin
val evens = (1..10) .filter { it % 2 == 0 } .map { it * it }
Rust — lazy, then collect
let evens: Vec<i32> = (1..=10) .filter(|x| x % 2 == 0) .map(|x| x * x) .collect();
KotlinRust
.map { }.map(|x| ...)
.filter { }.filter(|x| ...)
.forEach { }.for_each(...) or a for loop
.fold(0) { acc, x -> }.fold(0, |acc, x| ...)
.sumOf { }.map(...).sum()
.firstOrNull { }.find(...) → returns Option
.groupBy { }manual with HashMap, or the itertools crate
.sortedBy { }.sort_by_key(...) (in place, on a Vec)

Iterators are lazy — nothing runs until a consuming call (collect, sum, for, count). .iter() borrows, .into_iter() consumes, .iter_mut() gives mutable refs.

Modules, visibility & packages

// A "crate" = a package (a lib or binary). Cargo.toml is your build.gradle. mod network { // module, like a Kotlin package/file boundary pub fn connect() {} // `pub` = public; private by default mod internal { } // nested, private } use network::connect; // like Kotlin import connect();
KotlinRust
internal / private (default is public!)private by default, pub to expose
packagemod (modules) + crate
Gradle module / artifactcrate
build.gradle, Maven CentralCargo.toml, crates.io
import foo.Baruse foo::Bar;

Note the polarity flip: Kotlin is public-by-default, Rust is private-by-default.

Last updated on