Programming

Rust

What is Rust?

Rust started around Mozilla and is now a separate project. There is no garbage collector. Instead there is an ownership rule: every value has one owner. If the rule breaks, the program does not build. That stops many mistakes before the program runs.

What is Rust used for?

Rust shows up in a command-line tool, a network service, a browser piece, and a Wasm module.

  • A safer system tool
  • A fast API
  • Calling an existing C library
  • Work in the browser with Wasm

What can you do with Rust?

With Rust you write a tool and a service with less risk of a memory overrun. cargo builds the package and runs tests. The learning curve is steep, so Python stays shorter for a small script.

Is Rust hard to learn?

The first week is hard. When the borrow checker rejects code, the reason is a memory rule, not the syntax. After that, a refactor is calmer because many mistakes do not build.

Advantages and limits

Advantage: speed and memory safety sit together. Limit: the compiler is strict, build time can grow, and a GUI or a simple website is still not the first choice.

Basic ideas

Ownership is who owns a value. A borrow is a temporary look. String is growable text. &str is a view of text that lives somewhere else.

  • cargo new opens a project
  • A crate is a package
  • Result returns an error as a value
  • An unsafe block loosens the rule on purpose

A short example

This program writes one line to the terminal. The command is cargo run.

fn main() {
    println!("Hello");
}

Common questions

  • Has Rust replaced C++? Not everywhere. It is often chosen for a new system tool. Old code stays.
  • Is there a garbage collector? No. Memory is tracked by the ownership rule.
  • Is it as fast to learn as Python? No. The first week is steeper.