Learn Rust in Y Minutes
Learn Rust in Y Minutes
Rust is a programming language developed by Mozilla Research. Rust combines low-level control over
performance with high-level convenience and safety guarantees.
It achieves these goals without requiring a garbage collector or runtime, making it possible to use Rust
libraries as a “drop-in replacement” for C.
Rust’s first release, 0.1, occurred in January 2012, and for 3 years development moved so quickly that until
recently the use of stable releases was discouraged and instead the general advice was to use nightly builds.
On May 15th 2015, Rust 1.0 was released with a complete guarantee of backward compatibility.
Improvements to compile times and other aspects of the compiler are currently available in the nightly
builds. Rust has adopted a train-based release model with regular releases every six weeks. Rust 1.1 beta
was made available at the same time of the release of Rust 1.0.
Although Rust is a relatively low-level language, it has some functional concepts that are generally found in
higher-level languages. This makes Rust not only fast, but also easy and efficient to code in.
/// Documentation comments look like this and support markdown notation.
/// # Examples
///
/// ```
/// let five = 5
/// ```
///////////////
// 1. Basics //
///////////////
#[allow(dead_code)]
// Functions
// `i32` is the type for 32-bit signed integers
fn add2(x: i32, y: i32) -> i32 {
// Implicit return (no semicolon)
x + y
}
#[allow(unused_variables)]
#[allow(unused_assignments)]
#[allow(dead_code)]
// Main function
fn main() {
// Numbers //
// Immutable bindings
let x: i32 = 1;
// Integer/float suffixes
let y: i32 = 13i32;
let f: f64 = 1.3f64;
// Type inference
// Most of the time, the Rust compiler can infer what type a variable is,
so
// you don’t have to write an explicit type annotation.
// Throughout this tutorial, types are explicitly annotated in many places,
// but only for demonstrative purposes. Type inference can handle this for
// you most of the time.
let implicit_x = 1;
let implicit_f = 1.3;
// Arithmetic
let sum = x + y + 13;
// Mutable variable
let mut mutable = 1;
mutable = 4;
mutable += 2;
// Strings //
// String literals
let x: &str = "hello world!";
// Printing
println!("{} {}", f, x); // 1.3 hello world
// Vectors/arrays //
// A fixed-size array
let four_ints: [i32; 4] = [1, 2, 3, 4];
// Tuples //
// Destructuring `let`
let (a, b, c) = x;
println!("{} {} {}", a, b, c); // 1 hello 3.4
// Indexing
println!("{}", x.1); // hello
//////////////
// 2. Types //
//////////////
// Struct
struct Point {
x: i32,
y: i32,
}
let up = Direction::Up;
// Generics //
// Methods //
impl<T> Foo<T> {
// Methods take an explicit `self` parameter
fn bar(&self) -> &T { // self is borrowed
&self.bar
}
fn bar_mut(&mut self) -> &mut T { // self is mutably borrowed
&mut self.bar
}
fn into_bar(self) -> T { // here self is consumed
self.bar
}
}
trait Frobnicate<T> {
fn frobnicate(self) -> Option<T>;
}
/////////////////////////
// 3. Pattern matching //
/////////////////////////
match bar {
FooBar { x: 0, y: OptionalI32::AnI32(0) } =>
println!("The numbers are zero!"),
FooBar { x: n, y: OptionalI32::AnI32(m) } if n == m =>
println!("The numbers are the same"),
FooBar { x: n, y: OptionalI32::AnI32(m) } =>
println!("Different numbers: {} {}", n, m),
FooBar { x: _, y: OptionalI32::Nothing } =>
println!("The second number is Nothing!"),
}
/////////////////////
// 4. Control flow //
/////////////////////
// `for` loops/iteration
let array = [1, 2, 3];
for i in array.iter() {
println!("{}", i);
}
// Ranges
for i in 0u32..10 {
print!("{} ", i);
}
println!("");
// prints `0 1 2 3 4 5 6 7 8 9 `
// `if`
if 1 == 1 {
println!("Maths is working!");
} else {
println!("Oh no...");
}
// `if` as expression
let value = if true {
"good"
} else {
"bad"
};
// `while` loop
while 1 == 1 {
println!("The universe is operating normally.");
// break statement gets out of the while loop.
// It avoids useless iterations.
break
}
// Infinite loop
loop {
println!("Hello!");
// break statement gets out of the loop
break
}
/////////////////////////////////
// 5. Memory safety & pointers //
/////////////////////////////////
// Owned pointer – only one thing can ‘own’ this pointer at a time
// This means that when the `Box` leaves its scope, it can be automatically
deallocated safely.
let mut mine: Box<i32> = Box::new(3);
*mine = 5; // dereference
// Here, `now_its_mine` takes ownership of `mine`. In other words, `mine`
is moved.
let mut now_its_mine = mine;
*now_its_mine += 2;
println!("{}", now_its_mine); // 7
// println!("{}", mine); // this would not compile because `now_its_mine`
now owns the pointer
// Mutable reference
// While a value is mutably borrowed, it cannot be accessed at all.
let mut var2 = 4;
let ref_var2: &mut i32 = &mut var2;
*ref_var2 += 2; // '*' is used to point to the mutably borrowed
var2
Further reading
There’s a lot more to Rust—this is just the basics of Rust so you can understand the most important things.
To learn more about Rust, read The Rust Programming Language and check out the /r/rust subreddit. The
folks on the #rust channel on irc.mozilla.org are also always keen to help newcomers.
You can also try out features of Rust with an online compiler at the official Rust playpen or on the main Rust
website.
Got a suggestion? A correction, perhaps? Open an Issue on the Github Repo, or make a pull request yourself!
© 2020 P1start