General
Default integer type:
i32
Ownership
- Each resource can have only one owner at a time.
- References are variables which can access data without owning it.
Destructuring: Looks like pattern matching
References
&
: shared reference&mut
: mutable reference
Lifetime: Prefixed with an apostrophe.
- Eg:
'static
. - https://doc.rust-lang.org/nomicon/lifetimes.html
- Eg:
Box<T>
: simplest form of heap allocation- Provides ownership of allocated memory
Names ending with
!
are macros- Eg:
println!
, ~
- Eg:
&[i32]
: a 'slice'. A 'pointer' to an array (also contains info about the length of the array)Panic: like an error ???
unsafe { }
: code which won't be typechecked by compiler for type safety ??'
{:?}
formats the "next" value passed to a formatting macro, and supports anything that implementsDebug
.'Raw borrow:
&raw const
??Value expressions vs place expressions
- A subtle difference ?
- Place expression: like lvalue in C
- Value expression: like rvalue in C
- https://www.ralfj.de/blog/2024/08/14/places.html
Debug trait
Useful for printing values helpful for the programmer.
#[derive(Debug)]
enum List<T>{
Nil,
Cons(T, Box<List<T>>),
}
Misc
- TUI libraries: cursive, ratatui, crossterm
References
https://doc.rust-lang.org/rust-by-example/flow_control/match/destructuring/destructure_pointers.html
- Syntax is similar to pointers in C.
- Create a reference to
x
:&x
- Dereference a reference
rf
:*rf
Printing
See:
{:b} |
binary |
{:o} |
octal |
{:x} |
hexadecimal (lowercase) |
{:X} |
hexadecimal (uppercase) |
{:?} |
for debugging |
{} |
user friendly |
Copy semantics
- All stack allocated values have copy semantics.
- Types with copy semantics have
Copy
traits.Copy
andClone
traits are similar but have different purposes.- https://doc.rust-lang.org/std/marker/trait.Copy.html#whats-the-difference-between-copy-and-clone
Move semantics
https://doc.rust-lang.org/rust-by-example/scope/move.html
- Types with copy semantics have
Drop
trait (to drop ownership)- To prevent accidental use,
Drop::drop()
can't be called manually. - https://doc.rust-lang.org/std/ops/trait.Drop.html#you-cannot-call-dropdrop-yourself
- To prevent accidental use,
Example: Box
Box
has move semantics by default.
This works:
fn foo () {
let x = Box::new(5);
println!("x is: {}", x);
}
fn main() {
;
foo()}
/*
$ rustc hello.rs && ./hello
x is: 5
*/
This works too:
fn foo (x: Box<i32>) {
println!("x is: {}", x);
}
fn main() {
let x = Box::new(5);
foo(x);
//println!("x is: {}", x);
}
But this doesn't:
fn foo (x: Box<i32>) {
println!("x is: {}", x);
}
fn main() {
let x = Box::new(5);
// ownership of x 'moves' to foo. main no longer owns x
;
foo(x)
// main is trying to use something it doesn't own. Error!
println!("x is: {}", x);
}
Error is:
error[E0382]: borrow of moved value: `x`
--> hello.rs:9:24
|
7 | let x = Box::new(5);
| - move occurs because `x` has type `Box<i32>`, which does not implement the `Copy` trait
8 | foo(x);
| - value moved here
9 | println!("x is: {}", x);
| ^ value borrowed here after move
|
note: consider changing this parameter type in function `foo` to borrow instead if owning the value isn't necessary
--> hello.rs:2:12
|
2 | fn foo (x: Box<i32>) {
| --- ^^^^^^^^ this parameter takes ownership of the value
| |
| in this function
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable
|
8 | foo(x.clone());
| ++++++++
error: aborting due to previous error
For more information about this error, try `rustc --explain E0382`.
cargo
# --bin for binary program (--lib for library)
# initializes a git repo by default. Disabling with --vcs none
cargo new <proj_name> --bin --vcs none
# Or
cargo new <proj_name> --lib --vcs none
cargo build