Skip to content

Commit

Permalink
Feat: Add shadowing, borrowing and ownership works
Browse files Browse the repository at this point in the history
  • Loading branch information
pichtranst123 committed Jan 18, 2024
1 parent 08d6667 commit 2fb5180
Show file tree
Hide file tree
Showing 21 changed files with 108 additions and 36 deletions.
File renamed without changes.
36 changes: 0 additions & 36 deletions day_01_to_day_03/src/main.rs

This file was deleted.

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.
32 changes: 32 additions & 0 deletions src/day04_to_day06/borrowing.rs
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.
*/
3 changes: 3 additions & 0 deletions src/day04_to_day06/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod shadowing;
pub mod ownership;
pub mod borrowing;
10 changes: 10 additions & 0 deletions src/day04_to_day06/ownership.rs
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);
}
13 changes: 13 additions & 0 deletions src/day04_to_day06/shadowing.rs
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.
*/
}
50 changes: 50 additions & 0 deletions src/main.rs
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();

}

0 comments on commit 2fb5180

Please sign in to comment.