One month with Rust
Aimed at replacing C/C++
Not aimed at being an alternative to Haskell
I would consider it a competitor to Go
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin().read_line(&mut guess)
.ok()
.expect("Failed to read line");
println!("You guessed: {}", guess);
}
and control flow
Works as you'd expect, no parentheses
If trailing else, expression with last value in each
If not, expression of type ()
if condition {
} else if {
} else {
}
For-each style for, using Iterator trait
for var in expression {
..
}
for i in 0..10 {
// goes from zero to nine!
}
while statement and infinite loop, both unit type
while condition {
..
}
loop {
// prefer this to while "true"
}
let x = vec![0,1,2]
let y = x
println!("{}", x[0]) // will fail
Ownership of the vector was moved to y, so x is not longer a valid binding. rustc enforces this:
error: use of moved value: `x`
note: `x` moved here because it has type
`collections::vec::Vec<i32>`, which is moved by default
let y = x;
fun foo(v: Vec<i32>) {..}
let x = vec![0,1,2]
foo(x);
println!("{}", x[0]) // will fail
This will also fail, since ownership was moved to the function (as an argument)
Some types implement the Copy trait
indicating that they can freely be copied without introducing data races
Numeric types (no pointers)
let v = 1;
let v2 = v;
println!("{}", v); // works
What if we want to pass data to a function and then use it afterwards? The function could return it, but that would be very tedious. Instead we let the function borrow our data (implemented as a pointer).
fn foo(v: &Vec<i32>) {..}
let v = vec![0,1,2];
foo(v)
println!("{}", v[0]); // works
References are immutable by default, can't change data
fn foo(v: &Vec<i32>) {v.push(9);}
let v = vec![0,1,2];
foo(v)
println!("{}", v[0]);
error: cannot borrow immutable borrowed content `*v` as mutable v.push(9);
You can use a mutable reference, on both the argument and reference
fn foo(v: &mut Vec<i32>) {v.push(9);}
let v = vec![0,1,2];
foo(&mut v)
println!("{}", v[0]); // works
You cannot have mutable and immutable references at the same time
fn foo(v: &Vec<i32>) {..}
let v = vec![0,1,2];
let v2 = &mut v2
foo(v); // v2 is a mutable borrow,
//so can't pass immutable reference to function
If you limit the scope of the mutable borrow, it no longer exists at the point we want an immutable borrow.
fn foo(v: &Vec<i32>) {..}
let v = vec![0,1,2];
{
let v2 = &mut v2
}
foo(v); // works
You can have exactly one mutable reference,
or any number if immutable references,
but not both
Rust has traits, which are basically typeclasses
x