Follow TV Tropes

Following

Media Notes / Rust

Go To

https://static.tvtropes.org/pmwiki/pub/images/rustthrow.jpeg

Rust is a statically-typed, multi-paradigm, concurrent Programming Language with an emphasis on speed and safety. While a relatively new language, Rust has managed to be Github's most beloved programming language since 2016.

You can try out Rust for yourself here.

History

Rust began as side project of Mozilla employee, Graydon Hoare. Mozilla got involved in 2009 and sponsored the language once it was mature enough to run basic tests and demonstrate its core concepts. Featuring both Mozilla and Non-Mozilla developers, Rust is developed by a diverse and global community of enthusiasts. rustc (Rust's compiler) has over 1,000 unique contributors.

By 2011, Rust had successfully compiled itself and was shortly after released to the public in January, 2012.

Features Overview

These are some of features of Rust that help it stand out and what makes it increasingly popular for programmers used to C and C++. This isn't a comprehensive list however.

Expression based

In Rust, any part of the function body can be considered an expression, or something that returns a value. This allows for an implicit return of a value in a function by omitting the semi-colon where one would traditionally use the keyword return

Data ownership and lifetime

Part of how Rust achieves memory safety is enforcing a few rules regarding data ownership and lifetime. Ownership is the idea that some function owns the data. Once the function is done, if ownership was not explicitly transferred, then the data is automatically dropped from memory. Other functions can also borrow data, allowing a read-only reference to the data. However, if the ownership is transferred in the middle of a function, the function that used to own said data can no longer use it unless the new owner passes it back.

All of this is handled by the compiler by the way, meaning if the programmer doesn't keep up with this, the compiler will tell them of it.

Example of ownership

fn print_string(s: String) {
    println!("{}", s);
}

fn main() {
    let s = String::from("Hello, World");
    print_string(s); // s consumed by print_string
    // s has been moved, so cannot be used any more
    // another print_string(s); would result in a compile error
}

Memory Safety and Management

One standout feature of Rust is that it does not have a null value. This was done because null has wrecked havoc on a lot of other programming languages because you're not supposed to use null for anything, and using it either causes an outright error or unpredictable behavior. It's been called by its inventor The Billion Dollar Mistake

While not exactly a standout feature, but one that sets it apart from other languages made since the mid 90s is the lack of a garbage collector. Resources are taken through the Resource Acquisition Is Initialization (RIAA) principle and optionally through a reference counter. In addition, all variables have read-only access, requiring an explicit declaration of mut (for mutable) if the variable can be changed.

The Rust Compiler

The tool that turns Rust code into a program, and one that's rather powerful. The compiler handles all of the memory checking, which includes making that pointers have some reference to something before being used. By more or less forcing the programmer to fix their issues up front, programs written in Rust tend to be highly reliable from the get go.

The other feature of the compiler is that it's really helpful in pointing out what the problem is in a more useful way than most compilers, especially from popular ones like gcc for C (though gcc has gotten better at reporting issues with code).


Code Examples

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

Outputs Hello World to the console using the println! macro. You must wrap your program in the 'main' function.

    Variable Declaration and Concatenation 
fn main(){
    let a = 10;
    let b = 7;
    let c = "hi";
    println!("{}", a+b);
    // Outputs 17
    println!("{}", b+c);
    // Throws an error
    println!("Hello and {}", c);
    // Outputs Hello and hi
}

Rust uses the let keyword to declare variables for all data types; unusual for a low level language. Rust will not concatenate a string and a number automatically and will throw an error in the compiler.

    For Loops 

fn main(){
    for i in 0..10 {
println!("{}",i);
    }
}

Rust’s for loops work a bit differently than in other languages. In this example, 0..10 is an expression that takes a start and an end position, and gives an iterator over those values. The upper bound is exclusive though, so the loop will print 0 through 9, not 10.

Works Written in Rust

    Software 


Top