public:: true
- Cargo
- We can create a project using cargo new. We can build a project using cargo build. We can build and run a project in one step using cargo run. We can build a project without producing a binary to check for errors using cargo check. Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.
- Rust has statements and expressions, expressions return a value. If is an expression so you can bind the conditional to a variable. If a x+1 expression you add a ; it will become a statement
- Ownership Rules
- First, let’s take a look at the ownership rules. Keep these rules in mind as we work through the examples that illustrate them:
- Each value in Rust has an owner.
- There can only be one owner at a time.
- When the owner goes out of scope, the value will be dropped
- First, let’s take a look at the ownership rules. Keep these rules in mind as we work through the examples that illustrate them:
- Rust by Example book // online https://practice.rs
- Lifetime
- There are elision rules → that’s why for a lot of functions we don’t need to annotate lifetime
- but if we need, there are 2 rules
- Any reference must have an annotated lifetime
- Any reference being returned must have the same lifetime as one of the inputs or be static
-
fn pass_x<'a, 'b>(x: &'a i32, _: &'b i32) -> &'a i32 { x }
- Any reference must have an annotated lifetime
- Lifetime
- cargo-watch: to execute cargo commands each time files change
cargo watch -x test
- Difference between
CloneandCopytraits-
Copyis implicit, inexpensive, and cannot be re-implemented (memcpy).
-
Cloneis explicit, may be expensive, and may be re-implement arbitrarily.
-
- Difference between
Rc,BoxandRefCellRefCell→ is to remove the rule of “you can have n borrows non mutable or 1 mutable, for achieving this… inside uses unsafe code, so there is the possibility of crash in runtimeBox→ is a pointer to some data in the heap → prevents the Copy traitRC(Reference counting) → used on single thread, it allows to have multiple owners. Each time you use.clone()a counter is incremented, when all.clone()are out of scope the data can be drop.
- Resources: