Skip to content
Tutorials Center
My learning

Topics

Rust Reference ยท Concepts

Rust versus C++ and Java

Map familiar C++ and Java concepts to Rust: ownership versus RAII and GC, traits versus classes, enums, null, exceptions versus Result, generics, concurrency and build tools.

If you know C++ or Java, most of Rust's ideas have a counterpart, but a different default. Rust takes the memory control of C++ (no garbage collector, deterministic destruction) and adds compile-time checking of ownership; it takes the safety and tooling of Java but gives up inheritance, exceptions and null. This page maps concepts side by side so you can find the Rust spelling quickly.

The biggest mental shifts are: values move by default; references are checked by the borrow checker; behavior is attached through traits instead of class hierarchies; failures are values (Result, Option) rather than exceptions or null; and everything is immutable unless marked mut.

Rust
let name = String::from("Rust");
let moved = name;           // C++ would copy; Rust moves
// println!("{name}");      // error: value moved

Try it

Rust Ownership and RAII, without a GC

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Traits instead of inheritance, enums instead of null

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Rust Result instead of exceptions, and a data-carrying enum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with io::stdin().read_line()), it gets the next line from this box.

Concept map

Idea C++ Java Rust
Memory management RAII, new/delete, smart pointers garbage collector ownership and Drop; Box, Rc, Arc
Assignment of an object copies (unless moved) copies the reference moves (or copies if Copy)
Null nullptr, raw pointers null Option<T>; no null references
Errors exceptions, error codes, std::expected checked/unchecked exceptions Result<T, E>, ?; panic! for bugs
Inheritance classes, multiple inheritance classes, single inheritance none; traits and composition
Interfaces abstract classes, concepts interface trait
Generics templates (duck typed, checked at instantiation) generics with erasure generics with trait bounds, monomorphized
Runtime polymorphism virtual all non-final methods dyn Trait
Sum types std::variant sealed classes, records enum with data
Pattern matching switch, structured bindings switch patterns (recent) match, if let, let else
Const-ness const per use final per variable immutable by default, mut opt-in
Strings std::string, string_view String (UTF-16) String and &str (UTF-8)
Dynamic array std::vector ArrayList Vec<T>
Hash map unordered_map HashMap HashMap (SipHash by default)
Lambdas lambda with capture list lambdas closures (Fn, FnMut, FnOnce)
Namespaces namespace packages modules and crates
Headers .h/.cpp none none; pub items in modules
Threads std::thread, mutexes Thread, synchronized std::thread, Mutex<T> wraps the data, Send/Sync
Build and packages CMake, vcpkg, Conan Maven, Gradle Cargo, crates.io
Unsafe code everything Unsafe (rare) explicit unsafe blocks

Usage notes

  • Rust has no constructors or overloading. Use associated functions such as new, builders and Default; different behaviors get different names or generics.
  • No implicit conversions: an i32 never becomes an i64 on its own; see choosing a conversion.
  • Everything is an expression: if, match and blocks produce values; the last expression without a semicolon is the result.
  • Static methods and fields do not exist; use associated functions, const and static in impl blocks and modules.
  • Move semantics differ from C++: a moved-from Rust value is statically unusable (not a valid-but-unspecified state), and moves are plain memcpy with no move constructor.
  • No data race, no dangling reference in safe code; the price is learning lifetimes and the borrow rules. Cyclic data structures need Rc<RefCell<T>>, indexes or arenas.
  • Integer overflow panics in debug builds and wraps in release (like unsigned C++ arithmetic, unlike Java's always-wrapping); see integer overflow.
  • Compile times are longer than Java's and often similar to C++; incremental builds and cargo check help.
  • Interop: Rust calls C directly (FFI), and C++ interop goes through C-compatible layers or the cxx crate.