Cargo: The Rust Package Manager

Packages are called crates.

To create a new project: cargo new <project_name> . This creates a folder with a src/main.rs file (for executable crates).

Variables and Mutability

Declare variables with let. Example:

let x = 5; // Immutable
let mut y = 10; // Mutable

Be careful with referencing mutable and immutable variables:

Typing

Expressions and Statements

Borrowing and Ownership

Ownership ensures memory safety without a garbage collector:

Borrowing refers to taking a reference to an object:

  1. Immutable borrow:

    let s = String::from("hello");
    let r1 = &s; // Immutable borrow
    
  2. Mutable borrow (only one allowed at a time):

    let mut s = String::from("hello");
    let r2 = &mut s; // Mutable borrow+
    

<aside> 💡

Borrowing Rules

  1. Can either only have a single mutable reference OR have any number of immutable references.
  2. References must always be valid (within scope). </aside>

Traits

Traits provide a way to define shared behavior across different types.

Similar to Java's interfaces; they define shared behavior: