The Rust
Programming Language
Safe - Concurrent - Fast
Who Am I?
- Davis Silverman
-
Some PL Geek
- Hobby Game Dev
What Is RUST?
-
Modern idioms, the best of all worlds
- Immutable by default
- No null pointers
- Zero cost abstractions
- Strong ownership system
- move semantics by default - affine types
- pass by value or reference
- Memory safe without garbage collection
- Library-provided smart pointers!
-
Hygienic Macros
- Typesafe Metaprogramming!
Pop quiz: How many bits in a C `long double`?
Who Is behind Rust?
- Mozilla Research
- The Rust community!
Why?
-
A safe systems programming language
- Appropriate for large scale client applications
- Web browsers, servers, video games
- Not just `Safe C++`, modern language design.
An Introduction
fn fizzbuzz(num: uint) -> String {
match (num % 3, num % 5) {
(0, 0) => "FizzBuzz".to_string(),
(0, _) => "Fizz".to_string(),
(_, 0) => "Buzz".to_string(),
(_, _) => num.to_string()
}
}
fn main() -> () {
for num in range(0u, 101) {
println!("{}", fizzbuzz(num));
}
}
Nouns and verbs
struct Dog {
name: String,
age: uint,
}
impl Dog {
fn new(name: String, age: uint) -> Dog {
Dog {name:name, age:age}
}
fn bark(&self) -> String {
format!("I am dog {}. I am {}", self.name, self.age)
}
}
fn main() -> () {
let hunter_dawg: Dog = Dog::new("Hunter".to_string(), 5);
println!("{}", hunter_dawg.bark()) // See no ;
}
OPTION
PROBLEM SOLUTION
- Null pointers suck!
- Exceptions are expensive!
- Error codes (-1) are not strongly typed!
- Null pointers suck!
- Exceptions are expensive!
- Error codes (-1) are not strongly typed!
- Explicit error handling
- Efficiently represented!
- Strongly typed!
enum Option<T> {
Some(T),
None
}
enum Result<T, E> {
Ok(T),
Err(E)
}
Algebraic data types
#[deriving(Show)]
enum List<T> {
Nil,
Cons(T, Box<List<T>>),
}
fn main() {
let list: List<i32> = Cons(1i32, box Cons(2, box Cons(3, box Nil)));
println!("{}", list);
}
- Enum is awesome
- Box<T> is a pointer!
- let bindings
- automatic inference of integers
Ownership And Aliasing
// This function takes ownership of the heap allocated memory
fn destroy_box(c: Box<int>) {
// Have to return it to be claimed again.
println!("destroying a box that contains {}", c);
// `c` will be destroyed in this scope, and the memory will be freed
}
fn borrow_variable<T: std::fmt::Show>(d: &T) {
// Do not have to return the variable to let the caller reclaim it
println!("variable, {} is borrowed!", d);
}
fn borrow_mut_variable(f: &mut f64) {
*f = 42.0;
}
- References
- Mutable references
- Library defined pointers
Concurrency!
- Rust eliminates data races through its safety system
- Either have unique ownership and mutability
- Or shared ownership and immutability
fn main() {
for i in range(0u,500) {
spawn(proc() {
println!("Hello, {}!", i);
});
}
}
http://is.gd/TMCqCN
Problems
- The compiler is your worst enemy....In a good way!
- APIs constantly changing to_str -> to_string (this just got fixed :D )
-
Language has ergonomic issues - not a priority for pre 1.0 per se
Lifetime Annotations
Explicit lifetimes to keep track of references
struct Pair<'a, 'b> {
one: &'a mut int,
two: &'b mut int,
}
fn main() {
let mut one = 1; //lifetime of 'o
{
let mut two = 2; //lifetime of 't, shorter because in block ('t < 'o)
println!("Before: ({}, {})", one, two);
// `Pair` gets specialized for `'a = 'o` and `'b = 't`
let pair = Pair { one: &mut one, two: &mut two };
*pair.one = 2;
*pair.two = 1;
println!("After: ({}, {})", pair.one, pair.two);
}
}
Interesting projects!
- Gamedev
- Emulators
-
JBA(Rust branch) By Mozilla researcher Alex Chriton
- SprocketNES By Mozilla researcher Patrick Walton
- Web Browsers
- Servo By Mozilla - Multithreaded Browser engine
- HUGE AMOUNTS OF OTHER STUFF!
please read more
Amazing resources
Thank you!
Rust is great :) Questions?
Davis Silverman
sinistersnare@gmail.com
The Rust programming Language
By Davis Silverman
The Rust programming Language
- 6,227