-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from BhupalPrajapati/git@github.com-BhupalPraja…
…pati/Rust-Full-Code-from-Beginner-to-Advanced.git Git@GitHub.com bhupal prajapati/rust full code from beginner to advanced.git
- Loading branch information
Showing
16 changed files
with
284 additions
and
1 deletion.
There are no files selected for viewing
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,22 @@ | ||
name: Rust | ||
|
||
on: | ||
push: | ||
branches: [ "main" ] | ||
pull_request: | ||
branches: [ "main" ] | ||
|
||
env: | ||
CARGO_TERM_COLOR: always | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Build | ||
run: cargo build --verbose | ||
- name: Run tests | ||
run: cargo test --verbose |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,8 @@ | ||
[package] | ||
name = "concurrency" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
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,7 @@ | ||
mod message_passing; | ||
mod ownership; | ||
mod shring_state; | ||
mod sync_through_barries; | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
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,49 @@ | ||
// messagin gpasssing is done in rust by the mpsc channel(multiple producers and single consumer ) | ||
|
||
use std::{sync::mpsc,thread}; | ||
fn main() { | ||
let(tx, rx) = mpsc::channel(); | ||
|
||
// let using the muilple value , for that using the loops | ||
for i in 0..5{ | ||
// for the using of multiple threads, you need to create the clone of that varibale | ||
// here we are trying to create the clone of the tx variable | ||
|
||
let tx_clone = tx.clone(); | ||
thread::spawn(move || { | ||
let val = String::from("hi"); | ||
println!("Send Message :{val}"); | ||
tx_clone.send(val).unwrap(); | ||
// when 1st time message/thread is send is send then it is not available again | ||
|
||
// println!("{val}"); // there is show error in that condition, show no longer avialbe that vl bcz it is avl bcz it is already moved | ||
}); | ||
} | ||
|
||
|
||
// Note | ||
// The main thread of the transmiter is runing in the system, so we need to the drop the that thread after the receiver is end | ||
|
||
drop(tx); | ||
|
||
|
||
/* | ||
let receiver_value = rx.recv().unwrap(); // that line is onky received the only one message | ||
println!("Received Message :{}",receiver_value); | ||
// adding the another line to receive the messsage for the another thread | ||
let receiver_value = rx.recv().unwrap(); | ||
println!("Received Message :{}",receiver_value); | ||
*/ | ||
|
||
|
||
// to received all the message we need to treat the receiver like the iterator | ||
|
||
for message in rx{ | ||
println!("Received Message :{}",message); | ||
} | ||
|
||
|
||
|
||
} |
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,14 @@ | ||
use std::thread; | ||
fn main(){ | ||
let x = "some value".to_string(); | ||
println!("{}", x); | ||
thread::spawn( ||{ | ||
let y = x; | ||
println!("{y}"); | ||
}); | ||
} | ||
|
||
// fn main(){ | ||
// let x = 10; | ||
// println!("{}",x); | ||
// } |
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,62 @@ | ||
/* | ||
use std::sync::Mutex; | ||
use std::thread; | ||
fn main(){ | ||
let x = Mutex::new(5); | ||
{ | ||
let mut num = x.lock().unwrap(); | ||
*num =10; | ||
println!("{num}"); | ||
} | ||
let lock_m = x.lock().unwrap(); | ||
println!("{}",*lock_m); | ||
} | ||
*/ | ||
|
||
|
||
// consider a file which need to accessed to the multiple files | ||
|
||
|
||
use std::thread; | ||
// use std::rc::Rc; | ||
use std::sync::Arc; | ||
use std::sync::Mutex; | ||
struct File{ | ||
text:Vec<String>, | ||
} | ||
fn main(){ | ||
// create the instance of the file | ||
let file = Arc::new(Mutex::new(File{text:vec![]})); // The mutex is insure the text is updated by single thread at any given time | ||
|
||
// create thread handle to strore the thread vec | ||
let mut thread_vec = vec![]; | ||
|
||
for i in 0..10{ | ||
|
||
// let make the clone of the file | ||
let file = Arc::clone(&file); | ||
|
||
let handle = thread::spawn(move|| { | ||
|
||
// first acquire the lock | ||
let mut file_lock = file.lock().unwrap(); | ||
file_lock.text.push(format!("Hello{i}")); | ||
}); | ||
|
||
// finally add the thread to the thread vector | ||
thread_vec.push(handle); | ||
|
||
} | ||
|
||
// make sure when you goes for the completion of the all threads, we need to the join the these thread | ||
|
||
for handle in thread_vec{ | ||
handle.join().unwrap(); | ||
} | ||
|
||
// to access the file log, you need to call the lock the file | ||
let file_lock = file.lock().unwrap(); | ||
println!("{:?}",file_lock.text); | ||
|
||
} |
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,49 @@ | ||
use std::sync::{Mutex, Arc, Barrier}; | ||
use std::thread; | ||
|
||
fn main() { | ||
// we want to do in this program when task1 is completed, then only work on task2 in seq order | ||
|
||
let mut thread_vec = Vec::new(); | ||
|
||
// for e.g., we have two tasks then we take the barrier how to work on that condition | ||
|
||
let tasks = Arc::new(Mutex::new(vec![])); | ||
|
||
// to call the barrier we need to create a new constructor function | ||
|
||
let barrier = Arc::new(Barrier::new(5)); // the input in the barrier indicates the number of threads need to reach the barrier | ||
|
||
for i in 0..5 { | ||
// each task is used to clone the task | ||
let tasks = tasks.clone(); | ||
|
||
// we passed the clone of the barrier into each task | ||
let cloned_barrier = barrier.clone(); | ||
|
||
let handle = thread::spawn(move || { | ||
// we are trying to gain access to the task1 and put value in the vector | ||
// task1 | ||
tasks.lock().unwrap().push(format!("{0} Hello From Task 1", i)); | ||
|
||
// to create the barrier, we need to pass the wait function on it. | ||
cloned_barrier.wait(); | ||
|
||
// when the task is completely completed, then only work for the task2 | ||
// task2 | ||
tasks.lock().unwrap().push(format!("{0} Hello From Task 2", i)); | ||
}); | ||
thread_vec.push(handle); | ||
} | ||
|
||
for handle in thread_vec { | ||
handle.join().unwrap(); | ||
} | ||
|
||
// to gain access to these tasks we need to first lock on it | ||
// to access the inner value, we need to dereference | ||
let take_lock = &*tasks.lock().unwrap(); | ||
for contants in take_lock { | ||
println!("{}", contants); | ||
} | ||
} |
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,8 @@ | ||
[package] | ||
name = "real_life_problem_with_dsa" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
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,8 @@ | ||
[package] | ||
name = "understanding_size_in_rust" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] |
Binary file not shown.
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,5 @@ | ||
mod optionaly_sized; | ||
mod size; | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
Binary file not shown.
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,19 @@ | ||
struct Usensizedstruct<'a> { | ||
sized_struct: i32, | ||
unsized_struct: &'a [i32], // Using a reference to a slice | ||
} | ||
|
||
pub fn main() { | ||
// Create an array | ||
let array = [1]; | ||
|
||
// Create an instance of the struct | ||
let x = Usensizedstruct { | ||
sized_struct: 1, | ||
unsized_struct: &array, | ||
}; | ||
|
||
// Access fields of the struct | ||
println!("sized_struct: {}", x.sized_struct); | ||
println!("unsized_struct: {:?}", x.unsized_struct); | ||
} |
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,20 @@ | ||
// Briing the memory module in the Scope | ||
use std::mem::size_of; | ||
pub fn main(){ | ||
println!("hello world"); | ||
// sized types | ||
println!{"size of the array is:{}",size_of::<i32>()}; | ||
struct pool{ | ||
x:bool, // bool = 1 bit | ||
y: i64, // 8 bit(i32 = 4bit) | ||
} | ||
println!("pool size is: {}",size_of::<pool>()); | ||
|
||
|
||
// Unsized types | ||
|
||
// specially slices are unsized types in rust | ||
|
||
println!{"[i32] size is :{}",size_of::<&[i32]>()}; | ||
let a:[i32;3]; | ||
} |