Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
Rust released the alpha version of 1.0 recently, meaning there's finally a (mostly) stable standard library and feature set, so I figured it's about time we had a thread about it.

What is Rust?
Rust is a statically typed, compiled, systems programming language with a significant amount of safety built into the compiler. The goal of Rust is to be used in situations similar to where you would use C or C++, but provide features in the language to keep you safe from the hard parts of systems programming like leaking memory, race conditions, dangling pointers, and other. Rust has a heavy emphasis on zero cost abstractions in the language to provide all that safety. The compiler enforces these safety guarantees so that you can be reasonably sure that if your code compiles, then your code is correct and safe. Since this safety is enforced at compile time your program can run just as fast as the C or C++ program, hence the zero (runtime) cost abstractions.

While Rust is a systems language and stays mostly true to the C abstract machine model, it also has a lot of influence from functional languages. Here is an example of some idiomatic Rust to some all even numbers from 1 to 1000:

Rust code:
fn main() {
    let sum = (1..1000)
        .filter(|&x| x % 2 == 0)
        .fold(0, |a, b| a + b);
    println!("{}", sum)
}

// 249500
The filter and fold methods are in the standard library, and they accept lambdas which are written starting with the |var| syntax.

What makes Rust so safe?
The most defining feature in Rust is its ownership model. In Rust, each resource (stack memory, heap memory, file handle, struct, etc.) can only have one owner and this ownership is enforced by the compiler. A resource is automatically freed anytime its owner goes out of scope. The ownership model enforces that only one thing can make changes to a resource at any given time. If you require multiple objects to have ownership of a single resource, that can be introduced as necessary with per variable reference counting.

An owner of a resource can control the resource's deallocation, lend the resource out to borrowers, or move the ownership to a new owner. When lending a resource out (also known as borrowing), a resource can be borrowed immutably by any number of borrowers or borrowed mutably by one borrower. If a resource is borrowed immutably, no modifications can be made to that resource (by the borrowers or the owner). If a resource is borrowed mutably, only the borrower may modify the resource. All of this comes together to enforce memory safety without additional runtime baggage. This does, however, have compile time baggage and significant learning curve baggage as you struggle to satisfy the borrow checker.

Rust has runtime bounds-checking of Vector and array access, so no more straying off into random memory.

Rust strongly discourages unmanaged pointers by requiring the unsafe{} keyword around all usages. Safe code in Rust by default has no null values. Nullability can be added back in if required with the Option type that is so commonly used in functional languages:

Rust code:
fn print_value(value: Option<isize>) {
    match value {
        Some(val) => println!("The value is {}" , val),
        None => println!("That wasn't a number"),
    }
}

fn main() {
    let five = "5".parse(); // option type
    print_value(five);
    let garbage = "garbage".parse(); // option type
    print_value(garbage);
}

// The value is 5
// That wasn't a number
Note above, we also have the match keyword which requires exhaustive pattern matching. Every case must be checked (or a default provided) or the compiler will yell at you. Rust pattern matching is also used for one of my favorite FizzBuzz implementations:

Rust code:
fn main() {
    for i in 1..101 {
        match (i % 3, i % 5) {
            (0, 0) => println!("FizzBuzz"),
            (0, _) => println!("Fizz"),
            (_, 0) => println!("Buzz"),
            (_, _) => println!("{}", i),
        }
    }
}
Why should I care?
Because you're a bored and interested nerd like me. Because you've had your fair share of memory leaks and segfaults using unsafe languages. Because people on Hacker News keep talking about it and you want to offer your lovely opinion to the lovely discussion.

Where can I learn more about Rust?

Official Website: http://www.rust-lang.org/
Official Guidebook: http://doc.rust-lang.org/1.0.0-alpha/book/
Language Reference: http://doc.rust-lang.org/1.0.0-alpha/reference.html
Rust By Example: http://rustbyexample.com/

Adbot
ADBOT LOVES YOU

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
Oh gently caress me I'm bad at making threads. poo poo post ahoy.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
I don't think the new features of C++ are hampering it as much as the legacy cruft that is continuing to be supported along with it.

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
Here's a neat little project where they've built a Minecraft world renderer in Rust:

https://github.com/PistonDevelopers/hematite

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy

Vanadium posted:

Rust is taking heavy criticism on twitter for posting a "team" page that is apparently all dudes, mostly white, and one of the people organizing the Berlin usergroup is super frustrated now because all the negative publicity is making their job harder. :(

I don't know what the fuss is about, they're clearly including gorillas on the team: https://github.com/nrc

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
For a serious take on the matter, I'm loving tired of people coming in to a community from the outside and telling them they need to have more [race/gender/language/sexuality/etc]. Especially when that community is around an open source project that literally anyone can contribute to (assuming a decent contribution). Take this one person who replied to their team page tweet:

https://twitter.com/rustlang/status/599272106656985088

quote:

I've recently been learning and playing around with Rust, but have been severely put-off by total lack of women.

Maybe if you contributed to the project rather than bitching about the lack of women, there might actually be some women working on the language. Also, why the hell does the makeup of the core team matter for a programming language, which has primarily technical benefits and not social ones?

The only actual barrier to entry I can see for Rust contribution is the English language. But it's not like Rust is alone or even significant in that regard.

Adbot
ADBOT LOVES YOU

Bognar
Aug 4, 2011

I am the queen of France
Hot Rope Guy
Found this joke on the rust subreddit and enjoyed it:

quote:

The 1st rule of Rust Club is: you don't talk about Rust Club.

The 2nd rule of Rust Club is: error: 1st rule does not live long enough.

error: aborting due to previous error

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply