-
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 #2 from nihalxkumar/dev
Create for Rust and Update for C/Acknowledgements
- Loading branch information
Showing
5 changed files
with
133 additions
and
0 deletions.
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,3 @@ | ||
# Acknowledgements | ||
|
||
to be updated |
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,3 @@ | ||
# Acknowledgements | ||
|
||
to be updated |
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,34 @@ | ||
# Rust 🦀 | ||
|
||
"Rust is a systems programming language that runs blazingly fast, prevents segfaults, and guarantees thread safety." | ||
- <a href="https://www.rust-lang.org/" target="_blank">Rust Foundation</a> | ||
|
||
### 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 | ||
|
||
|
||
|
||
|
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,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 |
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,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<T: Trait1, U: Trait2>(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<T: 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<T, U>(param1: T, param2: U) | ||
where | ||
T: Trait1 + Trait2, | ||
U: Trait3 + Trait4 | ||
{ | ||
// code | ||
} | ||
``` | ||
|
||
```rust, ignore | ||
fn make_move<T>(thing: T, x: i32, y: i32) | ||
where T: Move | ||
{ | ||
thing.move_to(x, y); | ||
} | ||
``` |