Going through the work on:
https://doc.rust-lang.org/book/
URL: https://doc.rust-lang.org/stable/book/ch01-02-hello-world.html
fn main() { }
- Main class/startrustc main.rs
- Compiles the .rs fileprintln!();
- Print line,!
is used for macros
URL: https://doc.rust-lang.org/stable/book/ch01-03-hello-cargo.html
cargo new
- Set up a new projectcargo build
- Will build the executablecargo run
- Will compile and runcargo check
- Will check the source is goodcargo build --release
- Will build in release mode
URL: https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html
use std::io;
- Importing theio
library instd
let
string creates variables, immutable by default//
is a comment line starterString
is a growable UTF8 encoded bit of text::
indicates a function of the previous string, soString::new
calls the new function ofString
&mut guess
Passes the guess variable in as a mutable referenceResult
is a possible type returned from functions and is a type of enum- Each possible state of an enum is called a variant
Result
is used to encode error informationResult
's enum types areOk
andErr
Ok
variant indicates that the result was successful and the value is the successful value of calling the method/etcErr
variant indicates that the result failed and contains all the information about why it failed, etc.expect()
is a function which will. for anOk
result will return the value inOk
but otherwise stop the program with the message passed to the function with the various stack information
URL: https://doc.rust-lang.org/stable/book/ch03-01-variables-and-mutability.html
- By default variables are immutable
mut
can be used to make variables mutable- Constants can be defined with
const
and can never be mutable - Constants must have a type annotation and can only be set to values that can be evaluated at compile time.
- Variables can be shadowed in the same or different scopes with differing types and other data
URL: https://doc.rust-lang.org/stable/book/ch03-02-data-types.html
- Lots of standard data types
_
can be used as a number separator1_000_000
- Tuples can be defined with brackets
let tup : (bool, char, i128) = (true, 'W', 1231);
- They can be unpacked with let and ( )
let (w, x, y, z) = tup;
- Arrays are fixed size and can be typehinted
let array : [i64; 3] = [1, 3, 4];
URL: https://doc.rust-lang.org/stable/book/ch03-03-how-functions-work.html
- Function parameters requires the typehinting for each parameter
- Statements are instructions which perform some action and do not return
a value e.g.
let x = 10;
- Expressions evaluate to a value e.g.
x + 10
- Expressions do not end in a semicolon
- Calling a function or macro is an expression
- Functions that return values must be typehinted with an arrow
->
e.g.fn five() -> i32 { }
URL: https://doc.rust-lang.org/stable/book/ch03-05-control-flow.html
- if/else statements like other languages
- If condition must be a boolean, you can't put anything non-bool there, it won't auto typecast
- Since if is an expression you can use it on the right hand side of let
- e.g.
let number = if condition { 5 } else { 6 };
loop {}
can be used to run forever,break
will break out of this similar to other looping constructsloop {}
can return a value by using break to return the value.- Loops can be labelled with
'loop_label_name: loop {}
to allow picking which one to break - Breaking out of a specific labelled loop can be done with
break 'loop_label_name;
while condition {}
constructs work similar to other languages, albeit with caveats about the condition similar toif
- You can loop over items in a collection by using
for element in collection { }