Rust is a systems programming language focused on three goals: safety, speed, and concurrency. In this talk, you will get a high-level overview of the language, its syntax and semantics, and some code examples.
rand=“^0.3.0" # Use any release compatible with this specific version. Semantic Versioning rand=“=0.3.0" # Use only 0.3.0 exactly rand=“*" # Use latest version
i32 let x: i32 = 5; // Explicitly bound with the type i32 let (x, y) = (1, 2); // Pattern matching y = 3; // Raises an error. Immutable by default let mut y = 3; // y is now mutable and is bound to 3
3] let mut m = [1, 2, 3]; // m: [i32; 3] // Arrays have type [T; N] let a = [0; 20]; // a: [i32; 20] // Get the length println!("a has {} elements", a.len()); //> a has 20 elements
3] let mut m = [1, 2, 3]; // m: [i32; 3] // Arrays have type [T; N] let a = [0; 20]; // a: [i32; 20] // The `len` method yields the size of the array println!("a has {} elements", a.len()); //> a has 20 elements let names = ["Graydon", "Brian", "Niko"]; // names: [&str; 3] println!("The second name is: {}", names[1]); //> The second name is: Brian
&str, &str) // Indexing let v = tuple.1; println!("v is {}", v); // v is hello // Deconstructing let (x, y, z) = (1, 2, 3); println!("x is {}", x); // x is 1
main() { let mut point = Point { x: 0, y: 0 }; point.x = 5; println!("The point is at ({}, {})", point.x, point.y); let point = point; // this new binding can’t change now point.y = 6; // this causes an error }
+ 2); // Integer subtraction println!("1 - 2 = {}", 1 - 2); // Short-circuiting boolean logic println!("true AND false is {}", true && false); println!("true OR false is {}", true || false); println!("NOT true is {}", !true); // Use underscores to improve readability! println!("One million is written as {}", 1_000_000);
a vector let mut v = vec![1, 2, 3, 4, 5]; // v: Vec<i32> v.push(4); // Insert new element at the end of the vector // The `len` method yields the current size of the vector println!("Vector size: {}", v.len()); //> Vector size: 6 println!("Sixth element: {}", v[5]); //> Sixth element: 4
&str is a string slice. • String is a growable string. • "Hello there" is statically allocated string literal, typed string slice. • str.to_string() creates a new String, which is expensive. • string.as_slice() returns a view of the string, which is cheap.
println!("x is five!"); } else if x == 6 { println!("x is six!"); } else { println!("x is not five or six :("); } let y = if x == 5 { 10 } else { 15 }; // y: i32
in 0..10 { if x % 2 == 0 { continue 'outer; } // continues the loop over x if y % 2 == 0 { continue 'inner; } // continues the loop over y println!("x: {}, y: {}", x, y); } }
} impl Circle { // new is an associated function fn new(x: f64, y: f64, radius: f64) -> Circle { Circle { x: x, y: y, radius: radius, } } } fn main() { let c = Circle::new(0.0, 0.0, 2.0);