Rust
101:
- Scope Syntax:
- Everything between matched {}
- Scopes can nest {Outer Scope {Inner Scope}}
- Pay attention to a value’s scope!
Function Syntax:
fn myfunction { ... }
fn myfunction (arg: type, arg: type) - > resulttype { ... }
Type signatures are like Mad Libs
Function has at least name and scope
Macro Syntax:
Shorthand for functions with variable number of arguments
macroname!(foo, bar, baz)
doc.rust-lang.org/beta/book/macros.html
You’ll see “println!”
Punctuation Matters:
Expressions end with a semicolon
Exception: bare expression on last line of function returns result
Spaces separate tokens: i32 is not i 32
Whitespace is mostly irrelevant
Control Flow Syntax:
Conditionals and loops are familiar
if x { ... }
loop { ... }
while x { ... }
for x in 1..100 { ... }
Match statements combine conditionals
Function Example:
fn halve(x: i32) -> i32 {
return x / 2;
}
fn main(){
println!("{}", halve(4));
}
Matching on a Variable:
fn main() {
let day = 19;
println!("January {} 2017 is:", day);
match day {
15 => println!("Travel to Hobart"),
16 | 17 => println!("Miniconf Time"),
18...20 => println!("The Conference"),
_ => println!("not LCA at all"),
}
}