-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Add shadowing, borrowing and ownership works
- Loading branch information
Showing
21 changed files
with
108 additions
and
36 deletions.
There are no files selected for viewing
File renamed without changes.
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
pub fn run() { | ||
let mut book = String::from("The Rust Programming Language"); | ||
|
||
read_book(&book); | ||
|
||
annotate_book(&mut book, " - What a nice book!"); | ||
|
||
read_book(&book); | ||
|
||
println!("I am still got a book: {}", book); | ||
} | ||
|
||
fn read_book(book: &String) { | ||
println!("Reading: {}", book); | ||
} | ||
|
||
fn annotate_book(book: &mut String, note: &str) { | ||
book.push_str(note); | ||
println!("Added annotate to book"); | ||
} | ||
|
||
|
||
/* | ||
Output: | ||
Đang đọc: The Rust Programming Language | ||
Đã add ghi chú vào book | ||
Đang đọc: The Rust Programming Language - Sách hay! | ||
Tôi vẫn sở hữu book: The Rust Programming Language - Sách hay! | ||
*/ | ||
/* | ||
book.push_str(note); thay đổi nội dung của book. | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod shadowing; | ||
pub mod ownership; | ||
pub mod borrowing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//ownership works | ||
|
||
pub fn run() { | ||
let my_banhmi = String::from("bánh mì"); | ||
eat_pizza(my_banhmi); | ||
} | ||
|
||
fn eat_pizza(banhmi: String) { | ||
println!("I am eating {}!", banhmi); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
pub fn run(){ | ||
//Init a variable | ||
let x = 5; | ||
let x = x * 2; | ||
println!("x = {}", x); | ||
/* | ||
In this example, let x = 5; initializes x with the value 5. Then, let x = x * 2; creates a new variable also named x, which "shadows" the original x. This isn't modifying the original x (that would be mutability), but rather creating a completely new variable with the same name. The new x has a value of 10 (5 * 2). | ||
*/ | ||
|
||
/*Key point: | ||
Shadowing is a useful tool in Rust, allowing you to reuse variable names without losing the immutability of the original variables and offering flexible transformation in terms of data types. | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
//Day 01 to day03 | ||
pub mod day01_to_day03; | ||
|
||
// use day01_to_day03::print; | ||
// use day01_to_day03::strings; | ||
// use day01_to_day03::functions; | ||
// use day01_to_day03::types; | ||
// use day01_to_day03::arrays; | ||
// use day01_to_day03::vars; | ||
// use day01_to_day03::cli; | ||
// use day01_to_day03::loops; | ||
// use day01_to_day03::vectors; | ||
// use day01_to_day03::structs; | ||
// use day01_to_day03::conditionals; | ||
// use day01_to_day03::enums; | ||
// use day01_to_day03::pointer_ref; | ||
|
||
//Day04 to day06 | ||
pub mod day04_to_day06; | ||
use day04_to_day06::shadowing; | ||
use day04_to_day06::ownership; | ||
use day04_to_day06::borrowing; | ||
|
||
|
||
fn main() { | ||
//Hello world in rust | ||
// println!("Hello, world!"); | ||
//Day 01 to day03 | ||
|
||
// //Print from an another file! | ||
// print::run(); | ||
// strings::run(); | ||
// functions::run(); | ||
// types::run(); | ||
// arrays::run(); | ||
// vars::run(); | ||
// cli::run(); | ||
// loops::run(); | ||
// vectors::run(); | ||
// structs::run(); | ||
// conditionals::run(); | ||
// enums::run(); | ||
// pointer_ref::run(); | ||
|
||
//Day04 to day06 | ||
// shadowing::run(); | ||
// ownership::run(); | ||
borrowing::run(); | ||
|
||
} |