Learning rust: https://doc.rust-lang.org/book/ch01-01-installation.html
rustup doc
- local documentationcargo doc --open
- local documentation that includes documentation for all the crates you happen to have included in your project dependencies listing- Default (windows) install location:
%userprofile%\.cargo\bin
Rust is a statically typed language (all variable data types must be known at compile time).
rustup update
rustc main.rs
- to compileprintln!
-!
indicates we are calling a macro, not a function
cargo
is the package manager & build toolcargo new <project name>
- creates your boilerplate dir structure and hello worldcargo build
- use the--release
argument to include optimizationscargo run
- will also automatically build if src files changedcargo check
- to compile without overwriting your exe (to check for compilation errors)
use std::io;
- library importlet
- declare immutable variablelet mut
- declare mutable variable::
- allows traversing library > class/type > function&
indicates "by reference" for input argument - e.g.io::stdin().read_line(&mut guess);
- crates.io - public package (crate) manager - like nuget.org or npmjs.com
cargo update
- updates packages to latest bug fix (based upon semantic versioning)- you will need to update the
Cargo.toml
file to explicitly upgrade to new feature versions or breaking versions
- you will need to update the
Common Programming Concepts
let
- to declare immutable variableconst
- to declare a constant, must have a data type annotation- unlike C#, constants can be declared like so:
const THREE_HOURS_IN_SECONDS: u32 = 60 * 60 * 3;
- where the compiler will evaluate the expression as part of the compilation, this way the source code is easier to understand (for a human) - but is ultimately compiled down to the constant we are used to seeing in other languages.s
- unlike C#, constants can be declared like so:
let x = 5; let x = x + 1;
- this is called shadowing- this allows us to keep
x
as immutable, while performing transformation operations on it (thanks to thelet
keyword) - as part of this, you can also change the data type of the variable (very cool), e.g.:
let spaces = " "; let spaces = spaces.len();
- shadowing can also be used with inner scopes and the values are maintained independent of the outer scope
- this allows us to keep
- variables fall into two categories:
- Scalar
- integers:
i8
,u8
,i16
,u16
,i32
,u32
,i64
,u64
,i128
,u128
,isize
,usize
- floating-points:
f32
,f64
(all are signed, f64 is the default for implicitly defined variables) - boolean:
let f: bool = false;
(true
|false
) - char:
let z: char = 'Z';
- 4 bytes in size, unicode scalar value
- integers:
- Compound
- tuple:
let tup: (i32, f64, u8) = (500, 6.4, 1);
fixed in size, cannot grow or shrink after declaration- to read values out of a tuple, you use "destructuring":
let (x, y, z) = tup; println!("The value of y is: {y}");
- or use dot-notation followed by the index value:
let five_hundred = tup.0;
- to read values out of a tuple, you use "destructuring":
- array:
let a = [1, 3, 44, 34];
elements must be of the same type, fixed length- allocated to the stack, not heap
- strongly typed:
let a: [i32; 5] = [1, 3, 44, 34, 44];
- initialized with a default value of '3':
let a: [3; 5];
- tuple:
- Scalar
fn
keyword is used to declare a function- order of function declarations do not matter
argument
- the concrete value passed into theparameter
of the function- Statements versus Expressions
- Statements do not return a value
- end with a semi-colon
- Expressions return a value, and thus can be used to declare variables as part of a statement
- do not end with a semi-colon
- Statements do not return a value
- functions with return values (expressions) must be declared using
->
type declarations:fn my_function() -> i32 { 5 }
- this function returns the 32 bit integer value of5
//
- nuff' said
if
expressions are not wrapped in parenthesesif
expressions must return abool
data type (unlike JavaScript or Ruby)- code blocks within
if
expressions are sometimes called "arms", same withmatch
statement code blocks let number = if 3 > 7 { 5 } else { 6 };
- this is a valid use of anif
expression, because it is an expression, it returns a value (unlike a statement)- loops:
loop
while
for
- loop labels can be used to break out of outer scoped loops from within inner scoped loops
- spread and reversal functions can be handy in for loops:
for number in (1..4).rev() {}