diff --git a/src/Learning-C/Acknowledgements.md b/src/Learning-C/Acknowledgements.md new file mode 100644 index 0000000..d3a6a4d --- /dev/null +++ b/src/Learning-C/Acknowledgements.md @@ -0,0 +1,3 @@ +# Acknowledgements + +to be updated diff --git a/src/Learning-Rust/Acknowledgements.md b/src/Learning-Rust/Acknowledgements.md new file mode 100644 index 0000000..d3a6a4d --- /dev/null +++ b/src/Learning-Rust/Acknowledgements.md @@ -0,0 +1,3 @@ +# Acknowledgements + +to be updated diff --git a/src/Learning-Rust/Introduction.md b/src/Learning-Rust/Introduction.md new file mode 100644 index 0000000..eb92ef1 --- /dev/null +++ b/src/Learning-Rust/Introduction.md @@ -0,0 +1,34 @@ +# Rust 🦀 + +"Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety." + - Rust Foundation + +### Specialities + +- High-level language features without performance penalties +- Program behaviors can be enforced at compile time +- Enhanced program reliability +- Built—in dependency management, similar to npm +- Quickly growing ecosystem of libraries +- First-class multithreading + - Compiler error to improperly access shared data +- Type system: + - Can uncover bugs at compile time + - Makes refactoring simple + - Reduces the number of tests needed +- Module system makes code separation simple +- Adding a dependency is 1 line in a config file +- Tooling: + - Generate docs, lint code, auto format + +### Industries + +- Web3 +- Embedded Software +- OS +- Database Internals +- Robotics + + + + diff --git a/src/Learning-Rust/Match.md b/src/Learning-Rust/Match.md new file mode 100644 index 0000000..10a3685 --- /dev/null +++ b/src/Learning-Rust/Match.md @@ -0,0 +1,26 @@ +# Match + +Match expressions are similar to if else if + +It matches only on same type and accounts every possibility. +Making code more robust + +It works on expressions instead of statements. Therefore, at the end of statement instead of `;` we will use `,`. + +```rust +fn main() { + let some_int = 3; + match some_int { + 1 => println!("its 1"), + 2 => println!("its 2"), + 3 => println!("its 3"), + _ => println!("its something else") + } +} +``` + +Match will be checked by the compiler, so if a new possibility is added, you will be notified when this occurs + +In contrast, else..if is not checked by the compiler. If a new possibility is added, your code may contain a bug + +Prefer using match over else..if when working with a single variable diff --git a/src/Learning-Rust/Traits_and_Generics.md b/src/Learning-Rust/Traits_and_Generics.md new file mode 100644 index 0000000..1b2c78c --- /dev/null +++ b/src/Learning-Rust/Traits_and_Generics.md @@ -0,0 +1,67 @@ +# Traits + +Traits define similar functionality for different types + +It can be used to standardize functionality across multiple different types. + +Standardization permits functions to operate on multiple different types. [ Code deduplication ] + +## Generic Functions + +Generic functions are functions that can operate on multiple different types. + +A function that can have a single parameter to operate on multiple different types. + +Trait is used as function parameter instead of data type. Function depends on existence of functions declared by trait. + +Efficient code. Automatically deduces the type of the parameter when new data type is used. + +3 types of Generic Syntaxes + +1. Using impl Trait. + * Any type that implements a trait. Go with this when you have small number of traits and small number of parameters. + ```rust, ignore + fn function(param1: impl Trait1, param2: impl Trait2) { + // code + } + ``` + +2. Usual + * A generic type `T` is constrained to implement a specific Trait `Trait1` and `U` is contrained to implement `Trait2`. + The function parameter must be of Type `T` and `U` and the function will only work if the parameters implement the traits. + + ```rust,ignore + fn function(param1: T, param2: U){ + // code + } + ``` + * Generic type `T` (used to define the type of the parameter) then the trait that the type must implement. + In the function parameters we set `thing` to be of type `T` + and then we call the `move_to` method on `thing`. + + ```rust, ignore + fn make_move(thing: T, x: i32, y: i32) { + thing.move_to(x, y); + } + ``` + +3. Usual using Where keyword [most preferred] + * A generic type `T` and `U` are used as parameters and then we use the `where` keyword to specify the constraints. + + ```rust, ignore + fn function(param1: T, param2: U) + where + T: Trait1 + Trait2, + U: Trait3 + Trait4 + { + // code + } + ``` + + ```rust, ignore + fn make_move(thing: T, x: i32, y: i32) + where T: Move + { + thing.move_to(x, y); + } + ```