Rust|Chapter 0 Introduction to Rust & cargo package manager

Rust Programming Language – Beginner’s Course Notes

1. Introduction to Rust

  • Type: Systems programming language (similar to C and C++)
  • Compilation: Compiled language → source code translated to machine code via a compiler
  • Origin:
    • Started as a side project by Graden in 2007
    • Named after the rust fungus
    • Sponsored by Mozilla in 2009
    • First stable version released in 2014

2. Why Rust Is Highly Admired

  • Stack Overflow Survey (7 consecutive years):
    • 86.1% of 65,000 Rust developers want to use it again next year
    • Contrast: <20% for MATLAB
  • Admired alongside: JavaScript, Python, TypeScript, HTML, CSS, SQL — but Rust ranks highest by margin

3. Companies Using Rust

  • Examples: Dropbox, Discord, Google (Go), npm, Amazon, Mozilla, Facebook, Microsoft
  • Notable Endorsement: The White House suggested shifting from C/C++ to Rust

4. Core Qualities of Rust

Rust balances four key qualities:

4.1 Speed

  • Comparable to C and C++
  • Low-level, close to hardware → fast execution

4.2 Safety

  • Focus on memory safety
  • Avoids common bugs found in C/C++ (e.g., buffer overflows)

4.3 Concurrency

  • Supports multi-threading
  • Prevents issues like data races and dangling references

4.4 Portability

  • Compile once → run on Windows, Linux, macOS
  • Executable files work across systems

5. Memory Management Approach

  • No garbage collection (unlike Python)
  • No manual memory allocation functions (unlike C)
  • Uses Ownership and Borrowing (covered in depth in Chapters 4 & 5)

6. Package Manager: Cargo

  • Similar to npm (Node.js) or pip (Python)
  • Used for building, running, and managing Rust projects

7. Getting Started: Installation & First Program

7.1 Rust Website

  • rust-lang.org
  • Resources: Performance, reliability, productivity
  • Use cases: CLI tools, WebAssembly, networking, embedded systems

7.2 Documentation & Tools

  • The Rust Book – main learning resource
  • Standard Library – covers macros, modules, functions
  • Tools: rustc (compiler), cargo (package manager)

7.3 Installation

  • Recommended: rustup
  • Linux/Mac: curl command from website
  • Windows: Download and run rustup-init.exe

7.4 Verify Installation

1
2
3
rustup --version      # e.g., rustup 1.27.0
rustc --version # e.g., rustc 1.77.1
cargo --version # e.g., cargo 1.77.1

8. Writing Your First Rust Program

8.1 Method 1: Without Cargo

  1. Create a folder and file:
    1
    2
    3
    mkdir rust-hello
    cd rust-hello
    code .
  2. Create hello.rs:
    1
    2
    3
    fn main() {
    println!("Hello, world!");
    }
  3. Compile and run:
    1
    2
    rustc hello.rs       # creates hello.exe
    ./hello # outputs: Hello, world!

8.2 Method 2: With Cargo

  1. Create a new project:
    1
    cargo new hello-project
  2. Project structure:
    • src/main.rs – contains default “Hello, world!” code
    • Cargo.toml – configuration file (name, version, edition, dependencies)
  3. Modify main.rs as needed
  4. Run the project:
    1
    2
    cd hello-project
    cargo run # compiles and runs automatically
  5. Cargo recompiles on every change automatically

9. Key Takeaway

  • Without Cargo: Manual recompilation with rustc after each change
  • With Cargo: cargo run handles compilation automatically

10. Next Topic

  • Primitive Data Types