Rust|Chapter 0 Introduction to Rust & cargo package manager

Rust|Chapter 0 Introduction to Rust & cargo package manager
agsdRust 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:
curlcommand from website - Windows: Download and run
rustup-init.exe
7.4 Verify Installation
1 | rustup --version # e.g., rustup 1.27.0 |
8. Writing Your First Rust Program
8.1 Method 1: Without Cargo
- Create a folder and file:
1
2
3mkdir rust-hello
cd rust-hello
code . - Create
hello.rs:1
2
3fn main() {
println!("Hello, world!");
} - Compile and run:
1
2rustc hello.rs # creates hello.exe
./hello # outputs: Hello, world!
8.2 Method 2: With Cargo
- Create a new project:
1
cargo new hello-project
- Project structure:
src/main.rs– contains default “Hello, world!” codeCargo.toml– configuration file (name, version, edition, dependencies)
- Modify
main.rsas needed - Run the project:
1
2cd hello-project
cargo run # compiles and runs automatically - Cargo recompiles on every change automatically
9. Key Takeaway
- Without Cargo: Manual recompilation with
rustcafter each change - With Cargo:
cargo runhandles compilation automatically
10. Next Topic
- Primitive Data Types
Comment
匿名评论隐私政策
TwikooWaline
✅ 你无需删除空行,直接评论以获取最佳展示效果



