Skip to Content
Cheat Sheet

Idiom translation cheat sheet

KotlinRust
val x = 5let x = 5;
var x = 5let mut x = 5;
x?.foo() ?: defaultx.map(|v| v.foo()).unwrap_or(default)
x!!x.unwrap() / x.expect("msg")
x ?: returnlet Some(x) = x else { return; };
if (x != null) { ... }if let Some(x) = x { ... }
when (x) { ... }match x { ... }
data class#[derive(Clone, Debug, PartialEq)] struct
sealed classenum (variants carry data)
object (singleton)module-level fns, or a static, or Lazy
companion objectimpl block with associated fns (Type::new)
interfacetrait
class Foo : Bar() (inheritance)composition + trait (no inheritance)
extension funimpl SomeTrait for Type
lazy { }std::sync::LazyLock (stable 1.80) / once_cell::sync::Lazy
require() / check()assert! / debug_assert! / return Err
throwreturn Err(...) (recoverable) or panic! (bug)
try/catchmatch on Result, or ? to propagate
List<T> (read-only)&[T] (slice)
MutableList<T>Vec<T>
Map<K,V>HashMap<K,V>
to/Pairtuple (a, b)
.let { }let binding, or .map() on Option
.also { }{ let _ = &x; ... x } / inspect (iterators)
.apply { }build the struct directly (fields are explicit)
buildString { }let mut s = String::new(); s.push_str(...);
Last updated on