Rust

Appeared in:
2006-2012
Influenced by:
Influenced:
Paradigm:
Typing discipline:
File extensions:
.rs, .rc
Versions and implementations (Collapse all | Expand all):
Programming language

Rust is a multiparadigm programming language developed by Mozilla Labs.

The development started in 2006; initially it was done by Graydon Hoare, with Mozilla Labs joining the project in 2009. The first supported release of the language Rust 0.1 was released in January 2012.

Rust aims to be a perfect language for developing large client-server applications which run over the Internet. The concept of the language is oriented towards creating and maintaining boundaries — both abstract and practical — which preserve integrity, availability and concurrency of the system. The creators of the language aim to collect the best features of existing languages and combine them while not adding any new untested elements.

Rust manuals stress that at the moment the language is under active development, so any existing features can (and likely will) be changed in later versions. The main features of Rust are supposedly:

  • C-like syntax but very different semantics.
  • memory safety: Rust disallows null pointers and wild pointers (ones which point to non-existing objects), and provides automatic storage management.
  • mutability control: the objects are immutable by default, and no mutable state can be shared across several tasks.
  • dynamic execution safety: task failure / unwinding, trapping and logging mechanisms.
  • typestate system allows to define complex invariants that hold over data structures and program states before and after executing certain actions.
  • explicit memory control, including layout and allocation control.
  • very lightweight tasks (coroutines) which require minimum of resources and thus are cheap to spawn in massive quantities.
  • static, native compilation which uses LLVM, including its optimization passes, and emits ELF / PE / Mach-O files if needed.
  • simple interface with C.
  • support of purely functional paradigm with functions as first-class objects.
  • support of object-oriented paradigm combined with structural typing (instead of nominative), which means that there is no classes-based hierarchy.

Rust logo
Rust logo

Examples:

Hello, World!:

Example for versions Rust 0.1
use std;
import std::io;

fn main() {
    io::println("Hello, World!");
}

Factorial:

Example for versions Rust 0.1

This example uses recursive factorial definition. Due to uint overflow values of 13! and larger are calculated incorrectly.

use std;
import std::io;

fn factorial(x: int) -> uint {
  if (x <= 1) {
    ret 1u;
  } else {
    ret (x as uint) * factorial(x - 1);
  }
}

fn main() {
  let i = 0;
  while i <= 16 {
    io::println(#fmt("%d! = %u", i, factorial(i)));
    i = i + 1;
  }
}

Fibonacci numbers:

Example for versions Rust 0.1

This example calculates Fibonacci numbers recursively.

use std;
import std::io;

fn fibonacci(x: int) -> int {
  if (x <= 2) {
    ret 1;
  } else {
    ret fibonacci(x - 1) + fibonacci(x - 2);
  }
}

fn main() {
  let i = 1;
  while i <= 16 {
    io::print(#fmt("%d, ", fibonacci(i)));
    i = i + 1;
  }
  io::println("...");
}