Skip to content

Commit

Permalink
Merge pull request #2 from nihalxkumar/dev
Browse files Browse the repository at this point in the history
Create for Rust and Update for C/Acknowledgements
  • Loading branch information
nihalxkumar authored Feb 24, 2024
2 parents d82eaea + d8fbe16 commit 5aa09c6
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Learning-C/Acknowledgements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Acknowledgements

to be updated
3 changes: 3 additions & 0 deletions src/Learning-Rust/Acknowledgements.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Acknowledgements

to be updated
34 changes: 34 additions & 0 deletions src/Learning-Rust/Introduction.md
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




26 changes: 26 additions & 0 deletions src/Learning-Rust/Match.md
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
67 changes: 67 additions & 0 deletions src/Learning-Rust/Traits_and_Generics.md
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);
}
```

0 comments on commit 5aa09c6

Please sign in to comment.