commit a66604eb6c365749d28129ce7b90d828fdf867d3 Author: Ashborn Date: Tue Dec 16 22:10:06 2025 +0545 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..eb5a316 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target diff --git a/Control_Flow/Cargo.lock b/Control_Flow/Cargo.lock new file mode 100644 index 0000000..e8d6868 --- /dev/null +++ b/Control_Flow/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "Control_Flow" +version = "0.1.0" diff --git a/Control_Flow/Cargo.toml b/Control_Flow/Cargo.toml new file mode 100644 index 0000000..456d7b5 --- /dev/null +++ b/Control_Flow/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "Control_Flow" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/Control_Flow/README.md b/Control_Flow/README.md new file mode 100644 index 0000000..ac4738d --- /dev/null +++ b/Control_Flow/README.md @@ -0,0 +1,3 @@ + A non-copy type is a type that can't be duplicated smply by copying its bits. +1. when you assign or pass a non-copy type, owenership is moved, not duplicated +2. After a move, you can no longer use the original valu, unless you explicity clone it back diff --git a/Control_Flow/src/main.rs b/Control_Flow/src/main.rs new file mode 100644 index 0000000..895b203 --- /dev/null +++ b/Control_Flow/src/main.rs @@ -0,0 +1,60 @@ +fn main() { + /* + In rust, Control flow is how your program makes decisions and executes code conditionally or repeatedly. + */ + + //1. If Expression + println!("if expression"); + let mut num = 5; + if num < 15 { + println!("the {} is lesser than 15", num); + } else if num == 5 { + println!("the {} is 5", num); + } else { + println!("the number is prolly greater than 15") + } + + //2. match expression => generally its like a switch in other language, but more powerful and exhaustive + println!("match expression"); + match num { + 1 => println!("one"), + 2 | 3 => println!("two or three"), + 5 => println!("five"), + 4..=10 => println!("between 4 and 10"), //from start to end-1 but if =, then end is given there + _ => println!("Something else"), //default + } + + //3.loop => infinite, can break manually + println!("looping"); + loop { + num += 1; + if num == 10 { + break; + } + println!("loop count: {}", num); + } + + // 4. while loop -> loops while the conditions is true only + println!("while loop"); + while num != 0 { + println!("{}", num); + num -= 1; + } + + //for looping + println!("for looping"); + let arr = [3, 4, 5, 6, 1, 2, 34, 6, 78]; + for i in arr.iter() { + println!("{}", i); + } + + for i in 0..arr.len() { + println!("{}", arr[i]); + } + + /* + using the array or arra.iter() depends, the use of iter is just for read, that can be reused + and doesnt consumes array and is the refrence actually, but without it you have full control + and is completely reverse of the iter + */ +} diff --git a/Enums/Cargo.lock b/Enums/Cargo.lock new file mode 100644 index 0000000..f166eaf --- /dev/null +++ b/Enums/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "Enums" +version = "0.1.0" diff --git a/Enums/Cargo.toml b/Enums/Cargo.toml new file mode 100644 index 0000000..75d14f5 --- /dev/null +++ b/Enums/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "Enums" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/Enums/README.md b/Enums/README.md new file mode 100644 index 0000000..8698c75 --- /dev/null +++ b/Enums/README.md @@ -0,0 +1,5 @@ +# benefits of Enums with match: + +- compiler ensures all variants are handled +- clean and readable code +- can include data directly in the enum diff --git a/Enums/src/main.rs b/Enums/src/main.rs new file mode 100644 index 0000000..7b0cdb9 --- /dev/null +++ b/Enums/src/main.rs @@ -0,0 +1,35 @@ +// An enum defines a type by enumerating its possible values. EAch variant can optionally carry +// data + +enum Message { + Quit, // this is no data + Move { + //structure + x: i32, + y: i32, + }, + + Write(String), // string that can be written + ChangeColor(i32, i32, i32), // three integers, for the rgb +} + +fn main() { + let m1 = Message::Move { x: 1, y: 2 }; + let m2 = Message::Quit; + let m3 = Message::Write(String::from("safal lama")); + let m4 = Message::ChangeColor(255, 0, 0); + + handle(m1); + handle(m2); + handle(m3); + handle(m4); +} + +fn handle(msg: Message) { + match msg { + Message::Quit => println!("quit message received"), + Message::Move { x, y } => println!("moving ({}, {})", x, y), + Message::Write(txt) => println!("message: {}", txt), + Message::ChangeColor(r, g, b) => println!("Color: ({},{},{})", r, g, b), + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..d8aaac9 --- /dev/null +++ b/README.md @@ -0,0 +1,64 @@ +## 🟢 Beginner Level + +- [x] 1. Introduction to Rust & Installation +- [x] 2. Cargo and Crates +- [x] 3. Variables and Mutability +- [x] 4. Data Types +- [x] 4.5 Advanced => Strings, Array +- [x] 4.6 Advanced => Tuples +- [x] 4.7 Advanced => Vectors +- [x] 4.8 Advanced => Hashmaps +- [x] 5. Functions +- [x] 6. Control Flow (`if`, `match`, loops) +- [] 7. Ownership +- [] 8. References and Borrowing +- [] 9. Slices +- [] 10. Structs +- [] 11. Enums and Pattern Matching +- [] 12. `Option` and `Result` +- [] 13. Collections (`Vec`, `HashMap`, etc.) +- [] 14. Error Handling +- [] 15. Modules +- [ ] 15.1. File Structure +- [ ] 16. Packages, Crates, and Modules +- [ ] 17. Common Macros (`println!`, `dbg!`, `vec!`) + +--- + +## 🟡 Intermediate Level + +- [ ] 18. Traits and Trait Bounds +- [ ] 19. Generics +- [ ] 20. Lifetimes +- [ ] 21. Closures and Iterators +- [ ] 22. Smart Pointers (`Box`, `Rc`, `RefCell`) +- [ ] 23. Interior Mutability +- [ ] 24. Concurrency and Multithreading +- [ ] 25. Channels and Message Passing +- [ ] 26. Async Programming with `async/await` +- [ ] 27. Crates like `tokio`, `async-std` +- [ ] 28. Testing (`#[test]`, test modules) +- [ ] 29. Workspaces +- [ ] 30. Error Handling with `thiserror`, `anyhow` + +--- + +## 🔵 Advanced Level + +- [ ] 31. Procedural Macros +- [ ] 32. Unsafe Rust +- [ ] 33. FFI (Foreign Function Interface) +- [ ] 34. Advanced Lifetimes and HRTBs +- [ ] 35. Zero-Cost Abstractions +- [ ] 36. Pinning and `Unpin` +- [ ] 37. Custom Smart Pointers +- [ ] 38. Writing DSLs with Macros +- [ ] 39. Trait Objects and Dynamic Dispatch +- [ ] 40. Memory Layout and Alignment +- [ ] 41. Embedded Systems in Rust +- [ ] 42. Writing Web Servers (`actix-web`, `warp`, `axum`) +- [ ] 43. Building CLIs with `clap`, `structopt` +- [ ] 44. Writing Libraries and Publishing to `crates.io` +- [ ] 45. Performance Optimization & Benchmarking +- [ ] 46. Compiler Plugins and MIR (Mid-level IR) + diff --git a/Structs/Cargo.lock b/Structs/Cargo.lock new file mode 100644 index 0000000..3df070d --- /dev/null +++ b/Structs/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "Structs" +version = "0.1.0" diff --git a/Structs/Cargo.toml b/Structs/Cargo.toml new file mode 100644 index 0000000..8336199 --- /dev/null +++ b/Structs/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "Structs" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/Structs/src/main.rs b/Structs/src/main.rs new file mode 100644 index 0000000..b3044b5 --- /dev/null +++ b/Structs/src/main.rs @@ -0,0 +1,50 @@ +//Struct [Structure] is a custom data type that lets you group related values of different types +//under one name. + struct Person{ + name: String, + age: i32 + } + //structs with no fiels = unit like Structs + // struct Anime; + + impl Person { + fn greet(&self){ + println!("Hello, {}!", self.name); + } + + fn new(name: &str)->Person{ + Person { name: String::from(name), + age: 0 + } + } + } + + +fn main() { + + let mut person = Person{ + name: String::from("safal lama"), + age: 21 + }; + + + person.age+=1; + + + let fav = Person { + name: String::from("aur"), + ..person + }; + + + println!("{}, {}", fav.name, fav.age); + + + // let m = Anime; + println!("{} is {} yrs old", person.name, person.age); + + + let a = Person::new("Safal"); + a.greet(); + +} diff --git a/collections/Cargo.lock b/collections/Cargo.lock new file mode 100644 index 0000000..bf7db6a --- /dev/null +++ b/collections/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "collections" +version = "0.1.0" diff --git a/collections/Cargo.toml b/collections/Cargo.toml new file mode 100644 index 0000000..fd85423 --- /dev/null +++ b/collections/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "collections" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/collections/src/main.rs b/collections/src/main.rs new file mode 100644 index 0000000..bf21dfb --- /dev/null +++ b/collections/src/main.rs @@ -0,0 +1,32 @@ +//collections are data structures provided by the standard library that store multiple values. They +//are more flexible than arrays and tuples, especially when the size or contents of the collectoin +//are not knows at compile tiem. + +use std::collections::HashMap; + +fn main() { + //vector + let mut numbers = vec![1,2,3]; + numbers.pop(); + println!("{:?}", numbers); + + //hasmap + let mut scores = HashMap::new(); + scores.insert("alice", 1); + if let Some(s) = scores.get("alice"){ //key is case sensitive + println!("{}", s); + } + + + for val in &numbers{ + println!("{}", val); + } +} + +//when to use what? +//random access by index = vector +//fast insertin and removal = vecDeque +//need sorted keys = BTreeMap +//set of unique items = hasset +//prioirty queue = BinaryHeap + diff --git a/datatypes/Cargo.lock b/datatypes/Cargo.lock new file mode 100644 index 0000000..397170f --- /dev/null +++ b/datatypes/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "datatypes" +version = "0.1.0" diff --git a/datatypes/Cargo.toml b/datatypes/Cargo.toml new file mode 100644 index 0000000..a3d5cf3 --- /dev/null +++ b/datatypes/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "datatypes" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/datatypes/src/general.rs b/datatypes/src/general.rs new file mode 100644 index 0000000..c10e1a3 --- /dev/null +++ b/datatypes/src/general.rs @@ -0,0 +1,12 @@ +pub fn static_variables() { + //integer + let a: i32 = 4; + //float + let pi: f64 = 3.14; + //boolean + let iscorrect: bool = true; + //char + let c: char = 'A'; + println!("{}:{}:{}:{}", a, pi, iscorrect, c); +} + diff --git a/datatypes/src/hashmap.rs b/datatypes/src/hashmap.rs new file mode 100644 index 0000000..9eceae4 --- /dev/null +++ b/datatypes/src/hashmap.rs @@ -0,0 +1,22 @@ +use std::collections::HashMap; +pub fn hashmapf() { + /* + in rust, HashMap are the data types which can store according to the key value pair + */ + let mut scores = HashMap::new(); + scores.insert("blue", 10); + scores.insert("red", 100); + + println!("Blue: {:?}", scores.get("blue")); + + match scores.get("red") { + Some(x) => println!("Red => {}", x), + None => println!("error"), + } + + //for removing + scores.remove("blue"); + for (key, value) in &scores { + println!("{} => {}", key, value); + } +} diff --git a/datatypes/src/main.rs b/datatypes/src/main.rs new file mode 100644 index 0000000..fd65ddf --- /dev/null +++ b/datatypes/src/main.rs @@ -0,0 +1,12 @@ +mod general; +// mod string_array; +// mod tuples; +// mod vectors; +//mod hashmap; +fn main() { + general::static_variables(); + // string_array::s_a(); + // tuples::tuples(); + // vectors::vectors(); + // hashmap::hashmapf(); +} diff --git a/datatypes/src/string_array.rs b/datatypes/src/string_array.rs new file mode 100644 index 0000000..842dc33 --- /dev/null +++ b/datatypes/src/string_array.rs @@ -0,0 +1,26 @@ +pub fn s_a() { + //rust had two main string types + // 1. &str -> String slice which is immutable and usually used when reading static strings or string litreals + let str_slice: &str = "safal lama"; + println!("string slice: {}", str_slice); + + //2. String-> which is mutalbe and used when you need to modify or build strings at runtime + let mut str_val = String::from("safal"); + str_val.push_str("ski"); + println!("general String: {}", str_val); + + + //array-> is fixed size of same data type => stord on stack + let arr = ['a','b','c','d']; + println!("{:?}", arr); + println!("{}", arr[0]); + + for i in 0..arr.len(){ + print!("{}, ", arr[i]); + } + println!(); + for i in arr{ + print!("{}, ", i); + } + println!(); +} diff --git a/datatypes/src/tuples.rs b/datatypes/src/tuples.rs new file mode 100644 index 0000000..cde7020 --- /dev/null +++ b/datatypes/src/tuples.rs @@ -0,0 +1,14 @@ +pub fn tuples() { + /* + tuples are the data structure that can store multiple values of different types + */ + + let my_tup = (42, 3.14, 'R'); + println!("{:?}", my_tup); + println!("{}", my_tup.0); + + //tuples can be destructured + let (x, y, z) = my_tup; + println!("destructured: x={}, y = {}, z ={}", x, y, z); + +} diff --git a/datatypes/src/vectors.rs b/datatypes/src/vectors.rs new file mode 100644 index 0000000..7181c12 --- /dev/null +++ b/datatypes/src/vectors.rs @@ -0,0 +1,29 @@ +pub fn vectors() { + /* + in rust vectors Vec are growable, heap allocated arrays that store elements of the same + type. they are part of standard library and are commonly used when you need a dynamically + sized array. + [features] + - vectors are grow or shrink in sized + - elements are stored contiguiously in memory + - they may useful in Dref, index, Intollerator ets traits + */ + + //creating a vectors + let mut v: Vec = Vec::new(); //empty one[must be have to type anotate explictly] + let v1 = vec![1, 2, 3, 45]; + println!("{:?}", v1); + + //adding elements + v.push(12); + println!("{:?}", v); + + //accessing elements specific + println!("{}", v1[0]); + + //for fore safety like if index id out of bound to graceful handle stuff we use match + match v1.get(2) { + Some(value) => println!("value: {}", value), + None => println!("no value found at that index"), + } +} diff --git a/erro-handling/Cargo.lock b/erro-handling/Cargo.lock new file mode 100644 index 0000000..6bebe14 --- /dev/null +++ b/erro-handling/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "erro-handling" +version = "0.1.0" diff --git a/erro-handling/Cargo.toml b/erro-handling/Cargo.toml new file mode 100644 index 0000000..6daa2f1 --- /dev/null +++ b/erro-handling/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "erro-handling" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/erro-handling/README.md b/erro-handling/README.md new file mode 100644 index 0000000..a06e2b9 --- /dev/null +++ b/erro-handling/README.md @@ -0,0 +1,3 @@ +# for quick prototyping: +- let file = File::open("hello.txt").unwrap(); //panics +- let file = File::open("hello.txt").expect("Failed to open") // custom error diff --git a/erro-handling/src/main.rs b/erro-handling/src/main.rs new file mode 100644 index 0000000..e646266 --- /dev/null +++ b/erro-handling/src/main.rs @@ -0,0 +1,27 @@ +use std::{fs::File, io::{self, Read}}; + +//Error handling in Rust is a critical Aspect of writing reliable and robust programs. Rust +//encuorages handling errors explicity and provides two main types +// - panic! macro -> for unrecovable errors +// - Result enum -> for recovable errors +fn main() { + //always use panic when something goes wrong that the program cannot continue + // let num = vec![1,2,3]; + // println!("{}", num[99]); //panics cus the index is out of bound! + + //Result type is used to handle operations that can succeed or fail like file I/O + match read_file("hello.txt"){ + Ok(contents)=> println!("{}", contents), + Err(e)=> eprint!("Erro readihg file: {}",e) + } + + + +} + +fn read_file(filename: &str)-> Result{ + let mut file = File::open(filename)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + Ok(contents) +} diff --git a/functions/Cargo.lock b/functions/Cargo.lock new file mode 100644 index 0000000..8a72924 --- /dev/null +++ b/functions/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "functions" +version = "0.1.0" diff --git a/functions/Cargo.toml b/functions/Cargo.toml new file mode 100644 index 0000000..9c392ab --- /dev/null +++ b/functions/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/functions/src/main.rs b/functions/src/main.rs new file mode 100644 index 0000000..afc5bf6 --- /dev/null +++ b/functions/src/main.rs @@ -0,0 +1,20 @@ +/* +function is a block or reusable code. +defining the function if fn keyword, and every Rust programs starts with this main function. +*/ +fn main() { + greet(); + println!("{}", multiple(5, 5)); +} + +fn greet() { + println!("namaste!") +} + +/* +[parameterized function] +no type given is directly compliation error, so type annotatin is important +*/ +fn multiple(a: i32, b: i32) -> i32 { + a * b +} diff --git a/modules-file/Cargo.lock b/modules-file/Cargo.lock new file mode 100644 index 0000000..785c629 --- /dev/null +++ b/modules-file/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "modules-file" +version = "0.1.0" diff --git a/modules-file/Cargo.toml b/modules-file/Cargo.toml new file mode 100644 index 0000000..bff35bb --- /dev/null +++ b/modules-file/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "modules-file" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/modules-file/src/main.rs b/modules-file/src/main.rs new file mode 100644 index 0000000..9b4dbc8 --- /dev/null +++ b/modules-file/src/main.rs @@ -0,0 +1,13 @@ +mod math; +mod meth; + +fn main() { + // a module is a way to group related code together. You can define modules inline or sepearte + // file + let sum = math::add(2,3); + println!("{}", sum); + + //using that nested meth module + let mult = meth::advanced::mult(4, 5); + println!("{}", mult); +} diff --git a/modules-file/src/math.rs b/modules-file/src/math.rs new file mode 100644 index 0000000..d1ae09d --- /dev/null +++ b/modules-file/src/math.rs @@ -0,0 +1,3 @@ +pub fn add(a:i32, b:i32)->i32{ + return a+b; +} diff --git a/modules-file/src/meth/advanced.rs b/modules-file/src/meth/advanced.rs new file mode 100644 index 0000000..fb6dc3e --- /dev/null +++ b/modules-file/src/meth/advanced.rs @@ -0,0 +1,3 @@ +pub fn mult(i:i32, x:i32)->i32{ + return i*x; +} diff --git a/modules-file/src/meth/mod.rs b/modules-file/src/meth/mod.rs new file mode 100644 index 0000000..696d603 --- /dev/null +++ b/modules-file/src/meth/mod.rs @@ -0,0 +1 @@ +pub mod advanced; diff --git a/options-results/Cargo.lock b/options-results/Cargo.lock new file mode 100644 index 0000000..db9456f --- /dev/null +++ b/options-results/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "options-results" +version = "0.1.0" diff --git a/options-results/Cargo.toml b/options-results/Cargo.toml new file mode 100644 index 0000000..241537c --- /dev/null +++ b/options-results/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "options-results" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/options-results/src/main.rs b/options-results/src/main.rs new file mode 100644 index 0000000..d379bc7 --- /dev/null +++ b/options-results/src/main.rs @@ -0,0 +1,40 @@ +fn main() { + //Option and Result are enums used to handle absence of a value and error handling, + //respectively, withoug using null or exceptions +match get_username(1) { + Some(name)=>println!("{}", name), + None => println!("user not found") + +} + +match safe_divide(23, 2){ + Ok(result)=>println!("{}", result), + Err(e)=> println!("{}", e) +} + + +match safe_divide(23, 0){ + Ok(result)=>println!("{}", result), + Err(e)=> println!("{}", e) +} +} + + + +fn get_username(id:u32)->Option<&'static str>{ + if id ==1{ + Some("Safal") + }else{ + None + } +} + + +fn safe_divide(a:i32, b:i32)->Result{ + if b == 0 { + Err("Can't divide by zero".to_string()) + }else { + Ok(a/b) + } + +} diff --git a/ownership/Cargo.lock b/ownership/Cargo.lock new file mode 100644 index 0000000..8c5b86e --- /dev/null +++ b/ownership/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "ownership" +version = "0.1.0" diff --git a/ownership/Cargo.toml b/ownership/Cargo.toml new file mode 100644 index 0000000..a4b0049 --- /dev/null +++ b/ownership/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "ownership" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/ownership/src/main.rs b/ownership/src/main.rs new file mode 100644 index 0000000..a5e4f03 --- /dev/null +++ b/ownership/src/main.rs @@ -0,0 +1,51 @@ +fn main() { + //ownership in rust is a centrla concept that governs how memory is managed. It ensures memory + //safety without neding a garbage collector. + //- each value in rust has owner + //- when the owner goes out of scope, the value is dropped + //- values can be moved or borrowed, but not both at the same time + + let s1= String::from("safal"); + let s2 = s1; //ownership moves to s2 from s1 + println!("{}", s2); + + //now instead of moving, lets borrow + let a1 = String::from("safallama"); + safal(&a1); + println!("{}", a1); + + + + let maxxa = String::from("pantimos"); + takeownership(maxxa); //after this pulls up, the maxxa is dropped immediatedly + // + // + let maxxa = giveowenrship(); + println!("{}", maxxa); + + + + + + +} + +fn safal(s: &String){ + println!("{}", s); +} + + + +fn takeownership(s:String){ + println!("got string{}",s); +} + + + +fn giveowenrship()->String{ + return String::from("ifrate"); +} + + + + diff --git a/r_B/Cargo.lock b/r_B/Cargo.lock new file mode 100644 index 0000000..04574a5 --- /dev/null +++ b/r_B/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "r_B" +version = "0.1.0" diff --git a/r_B/Cargo.toml b/r_B/Cargo.toml new file mode 100644 index 0000000..a12e2fa --- /dev/null +++ b/r_B/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "r_B" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/r_B/src/main.rs b/r_B/src/main.rs new file mode 100644 index 0000000..a273e29 --- /dev/null +++ b/r_B/src/main.rs @@ -0,0 +1,29 @@ +fn main() { + //borrowing => It means to allowing a function or variabe to use a value without taking + //ownership : type: + //- immutable borrowing [read only access] + //- mutable borrowing [read write access] + let s = String::from("hello world"); + print_strirng(&s); + + let mut s1 = String::from("safal, hi"); + change_string(&mut s1); + println!("{}", s1); + + //ownership => It means every value in Rust has single owner, the variable responsible for it, + //When ownership changes, the previous owner can no longer use the value + + let owner = String::from("pantsu"); + let owner1 = owner; + println!("{}", owner1); +} + +//borrowing but read only +fn print_strirng(s: &String){ + println!("{} borrowed", s); +} + +fn change_string(s: &mut String){ + s.push_str("!!"); +} + diff --git a/slices/Cargo.lock b/slices/Cargo.lock new file mode 100644 index 0000000..5d47168 --- /dev/null +++ b/slices/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "slices" +version = "0.1.0" diff --git a/slices/Cargo.toml b/slices/Cargo.toml new file mode 100644 index 0000000..4ad77ed --- /dev/null +++ b/slices/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "slices" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/slices/src/main.rs b/slices/src/main.rs new file mode 100644 index 0000000..4a06331 --- /dev/null +++ b/slices/src/main.rs @@ -0,0 +1,27 @@ +fn main() { + //slices are a way to reference a contigous sequence of elements in a collection, without + //owning the data. they let you work with parts of arryas, vectors or strings + //- slices are view into collection + //-they don't own the data, just borrow it + //- &[T] for a slice of elements of type T + + let arr = [1,2,3,4,5,50]; + let slice = &arr[1..4]; + + println!("{:?}", slice); + + + //slice of vector + let vec = vec![2,3,4,5]; + let slicer = &vec[2..]; + println!("{:?}", slicer); + + //string slice + let str = String::from("safal lama"); + let slc = &str[0..]; + + println!("{:?}", slc); + + + //Slices allow safe, efficient access to parts of collections without copying data. +} diff --git a/variables_mutability/Cargo.lock b/variables_mutability/Cargo.lock new file mode 100644 index 0000000..4d88885 --- /dev/null +++ b/variables_mutability/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "variables_mutability" +version = "0.1.0" diff --git a/variables_mutability/Cargo.toml b/variables_mutability/Cargo.toml new file mode 100644 index 0000000..2cbd0f3 --- /dev/null +++ b/variables_mutability/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables_mutability" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/variables_mutability/src/main.rs b/variables_mutability/src/main.rs new file mode 100644 index 0000000..bd514f8 --- /dev/null +++ b/variables_mutability/src/main.rs @@ -0,0 +1,39 @@ +fn main() { + //variables by default is unchangable + //let is used to define the variables + let var1 = String::from("safal lama"); + println!("default: {}", var1); + //mut is used to create a changable variable + let mut var2 = String::from("this is safal lama"); + var2.push_str(". he is a huge fan of goku"); + println!("mutable: {}", var2); + + //shadowing is a process of redeclaring the variables + let var_shade = 1; + println!("{}", var_shade); + let var_shade = var_shade + 15; + println!("{}", var_shade); + let var_shade = "Safal lama"; //basically we can directly change the type of variable when shadowing + println!("{}", var_shade); + + //constants are not changable and they have to be specified with type annotation + const KONST: i32 = 1; + println!("{}", KONST); // use caps for betterment + + /* + [mutability and refrence] + only one mutable reference at a time or a multiple inmutable reference, but not both + */ + let mut str1 = String::from("kakarot"); + let str2 = &mut str1; //only one mutable as rule + str2.push_str(", i am goku!"); + println!("{}", str1); + + //[can be multiple] + let strx = &str1; + println!("{}", strx); + + /* [note] + The above code will work despite having mutable and inmutable both however, its about lifetime, cause one ends and other begins, if we didnt printed, then it wouldnt have worked. + */ +}