Skip to content

Commit

Permalink
Merge pull request #5 from BhupalPrajapati/git@github.com-BhupalPraja…
Browse files Browse the repository at this point in the history
…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
BhupalPrajapati authored Jun 22, 2024
2 parents a4f0860 + 8723ac1 commit a636a50
Show file tree
Hide file tree
Showing 16 changed files with 284 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .github/workflows/rust.yml
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
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
workspace = { members = ["src/macros", "src/web_scrapping_project"] }
workspace = { members = ["src/macros", "src/web_scrapping_project","real_life_problem_with_dsa","concurrency","understanding_size_in_rust"] }
[package]
name = "practicse"
version = "0.1.0"
Expand Down
8 changes: 8 additions & 0 deletions concurrency/Cargo.toml
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]
7 changes: 7 additions & 0 deletions concurrency/src/main.rs
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!");
}
49 changes: 49 additions & 0 deletions concurrency/src/message_passing.rs
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);
}



}
14 changes: 14 additions & 0 deletions concurrency/src/ownership.rs
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);
// }
62 changes: 62 additions & 0 deletions concurrency/src/shring_state.rs
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);

}
49 changes: 49 additions & 0 deletions concurrency/src/sync_through_barries.rs
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);
}
}
8 changes: 8 additions & 0 deletions real_life_problem_with_dsa/Cargo.toml
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]
8 changes: 8 additions & 0 deletions understanding_size_in_rust/Cargo.toml
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 added understanding_size_in_rust/src/main.exe
Binary file not shown.
5 changes: 5 additions & 0 deletions understanding_size_in_rust/src/main.rs
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.
19 changes: 19 additions & 0 deletions understanding_size_in_rust/src/optionaly_sized.rs
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);
}
20 changes: 20 additions & 0 deletions understanding_size_in_rust/src/size.rs
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];
}

0 comments on commit a636a50

Please sign in to comment.