rust language code base on https://doc.rust-lang.org/book/ch01-02-hello-world.html
This tutorial is base on The Rust Programming Language
- Foreword
- Introduction
- 1. Getting Started
- 2. Programming a Guessing Game
- 3. Common Programming Concepts
- 4. Understanding Ownership
- 5. Using Structs to Structure Related Data
- 6. Enums and Pattern Matching
- 7. Managing Growing Projects with Packages, Crates, and Modules
- 8. Common Collections
- 9. Error Handling
- 10. Generic Types, Traits, and Lifetimes
- 11. Writing Automated Tests
- 12. An I/O Project: Building a Command Line Program
- 12.1. Accepting Command Line Arguments
- 12.2. Reading a File
- 12.3. Refactoring to Improve Modularity and Error Handling
- 12.4. Developing the Library’s Functionality with Test Driven Development
- 12.5. Working with Environment Variables
- 12.6. Writing Error Messages to Standard Error Instead of Standard Output
- 13. Functional Language Features: Iterators and Closures
- 14. More about Cargo and Crates.io
- 15. Smart Pointers
- 15.1. Using Box to Point to Data on the Heap
- 15.2. Treating Smart Pointers Like Regular References with the Deref Trait
- 15.3. Running Code on Cleanup with the Drop Trait
- 15.4. Rc, the Reference Counted Smart Pointer
- 15.5. RefCell and the Interior Mutability Pattern
- 15.6. Reference Cycles Can Leak Memory
- 16. Fearless Concurrency
- 17. Object Oriented Programming Features of Rust
- 18. Patterns and Matching
- 19. Advanced Features
- 20. Final Project: Building a Multithreaded Web Server
- 21. Appendix
- Now open the main.rs file you just created and enter the code in Listing 1-1. Filename: main.rs
fn main() {
println!("Hello, world!");
}enter the following commands to compile and run the file:
❯ rustc main.rs
❯ rustc main.rs
❯ ./main
Hello, world!- Install Cargo and use Cargo to make projects.
❯ cargo --version
cargo 1.75.0 (1d8b05cdd 2023-11-20)- Creating a Project with Cargo
❯ cargo new hello_cargo
Created binary (application) `hello_cargo` package
❯ cd hello_cargo
- Building and Running a Cargo Project
❯ cargo build
Compiling hello_cargo v0.1.0 (/home/dc/5W/github/DC-Melo/The-Rust-Programming-Language/01-Getting
-Started/1.3.Hello-Cargo/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s
# run the executable with this command:
❯ ./target/debug/hello_cargo
Hello, world!
# we can also use cargo run to compile the code and then run the resultant executable all in one command:
❯ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/hello_cargo`
Hello, world!
# cargo check. This command quickly checks your code to make sure it compiles but doesn’t produce an executable:
❯ cargo check
Finished dev [unoptimized + debuginfo] target(s) in 0.00s- Building for Release
❯ cargo build --release
Compiling hello_cargo v0.1.0 (/home/dc/5W/github/DC-Melo/The-Rust-Programming-Language/01-Getting
-Started/1.3.Hello-Cargo/hello_cargo)
Finished release [optimized] target(s) in 0.15s- Setting Up a New Project
❯ cargo new guessing_game
Created binary (application) `guessing_game` package
dc in 🌐 dc-iMac in The-Rust-Programming-Language/02-Programming-a-Guessing-Game on 02.Programming-a-Guessing-Game [!?]
❯ cd guessing_game
dc in 🌐 dc-iMac in The-Rust-Programming-Language/02-Programming-a-Guessing-Game/guessing_game on 02.Programming-a-Guessing-Game [!?] is 📦
v0.1.0 via 🦀 v1.75.0
❯ cargo run
Compiling guessing_game v0.1.0 (/home/dc/5W/github/DC-Melo/The-Rust-Programming-Language/02-Programming-a-Guessing-Game/guessing_game)
Finished dev [unoptimized + debuginfo] target(s) in 0.16s
Running `target/debug/guessing_game`
Hello, world!- Processing a Guess Filename: src/main.rs tag: v0.02.1
use std::io;
fn main() {
println!("Guess the number!");
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
println!("You guessed: {guess}");
}❯ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
❯ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/guessing_game`
Guess the number!
Please input your guess.
10
You guessed: 10Filename: src/main.rs tag: v0.02.2
use rand::Rng;
use std::cmp::Ordering;
use std::io;
fn main() {
println!("Guess the number!");
let secret_number = rand::thread_rng().gen_range(1..=100);
loop {
println!("Please input your guess.");
let mut guess = String::new();
io::stdin()
.read_line(&mut guess)
.expect("Failed to read line");
let guess: u32 = match guess.trim().parse() {
Ok(num) => num,
Err(_) => continue,
};
println!("You guessed: {guess}");
match guess.cmp(&secret_number) {
Ordering::Less => println!("Too small!"),
Ordering::Greater => println!("Too big!"),
Ordering::Equal => {
println!("You win!");
break;
}
}
}
}Filename: Cargo.toml tag: v0.02.2
[dependencies]
rand = "0.8.0"
- Build project
❯ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
dc in 🌐 dc-iMac in The-Rust-Programming-Language/02-Programming-a-Guessing-Game/guessing_game on
02.Programming-a-Guessing-Game [!?] is 📦 v0.1.0 via 🦀 v1.75.0
❯ cargo run
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
Running `target/debug/guessing_game`
Guess the number!
Please input your guess.
10
You guessed: 10
Too small!