mirror of
https://github.com/Fechin/reference.git
synced 2026-11-21 13:34:06 +03:00
fix: rust formatting
Fixes the classic code block with | symbol issue
This commit is contained in:
1 file changed
+84
-140
+84
-140
@@ -4,16 +4,14 @@ date: 2022-01-01 11:51:44
|
||||
background: bg-black
|
||||
tags:
|
||||
categories:
|
||||
- Programming
|
||||
- Programming
|
||||
intro: |
|
||||
The Rust quick reference cheat sheet that aims at providing help on writing basic syntax and methods.
|
||||
The Rust quick reference cheat sheet that aims at providing help on writing basic syntax and methods.
|
||||
plugins:
|
||||
- copyCode
|
||||
- copyCode
|
||||
---
|
||||
|
||||
|
||||
Getting Started
|
||||
---------------
|
||||
## Getting Started
|
||||
|
||||
### Hello_World.rs
|
||||
|
||||
@@ -22,7 +20,9 @@ fn main() {
|
||||
println!("Hello, World!");
|
||||
}
|
||||
```
|
||||
|
||||
#### Compiling and Running
|
||||
|
||||
```shell
|
||||
$ rustc Hello_World.rs
|
||||
$ ./Hello_World
|
||||
@@ -31,19 +31,18 @@ Hello, World!
|
||||
|
||||
### Primitive types
|
||||
|
||||
| | |
|
||||
|---------------------------|----------------------------------|
|
||||
| `bool` | Boolean (`true` _/_ `false`) |
|
||||
| `char` | character |
|
||||
| `f32`, `f64` | 32-bits, 64-bits floats |
|
||||
| `i64`, `i32`, `i16`, `i8` | signed 16- ... integers |
|
||||
| `u64`, `u32`, `u16`, `u8` | unsigned 16-bits, ... integers |
|
||||
| `isize` | pointer-sized signed integers |
|
||||
| `usize` | pointer-sized unsigned integers |
|
||||
| | |
|
||||
| ------------------------- | :------------------------------ |
|
||||
| `bool` | Boolean (`true` _/_ `false`) |
|
||||
| `char` | character |
|
||||
| `f32`, `f64` | 32-bits, 64-bits floats |
|
||||
| `i64`, `i32`, `i16`, `i8` | signed 16- ... integers |
|
||||
| `u64`, `u32`, `u16`, `u8` | unsigned 16-bits, ... integers |
|
||||
| `isize` | pointer-sized signed integers |
|
||||
| `usize` | pointer-sized unsigned integers |
|
||||
|
||||
See: [Rust Types](#rust-types)
|
||||
|
||||
|
||||
### Formatting {.row-span-2}
|
||||
|
||||
```rust {.wrap}
|
||||
@@ -70,7 +69,6 @@ let x = "world";
|
||||
println!("Hello {x}!");
|
||||
```
|
||||
|
||||
|
||||
### Printing Styles
|
||||
|
||||
```rust
|
||||
@@ -87,7 +85,6 @@ eprint!("This is an error\n");
|
||||
eprintln!("This is an error with new line");
|
||||
```
|
||||
|
||||
|
||||
### Variables
|
||||
|
||||
```rust
|
||||
@@ -104,8 +101,6 @@ let (name, age) = ("ElementalX", 20);
|
||||
const SCREAMING_SNAKE_CASE:i64 = 9;
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Comments
|
||||
|
||||
```rust
|
||||
@@ -114,10 +109,9 @@ const SCREAMING_SNAKE_CASE:i64 = 9;
|
||||
/// Outer doc comments
|
||||
//! Inner doc comments
|
||||
```
|
||||
|
||||
See: [Comment](https://doc.rust-lang.org/reference/comments.html)
|
||||
|
||||
|
||||
|
||||
### Functions
|
||||
|
||||
```rust
|
||||
@@ -129,14 +123,10 @@ fn main(){
|
||||
test();
|
||||
}
|
||||
```
|
||||
|
||||
See: [Functions](#rust-functions)
|
||||
|
||||
|
||||
|
||||
|
||||
Rust Types
|
||||
--------------
|
||||
|
||||
## Rust Types
|
||||
|
||||
### Integer
|
||||
|
||||
@@ -147,7 +137,6 @@ let c: i64 = 8999;
|
||||
let d = -90;
|
||||
```
|
||||
|
||||
|
||||
### Floating-Point
|
||||
|
||||
```rust
|
||||
@@ -156,7 +145,6 @@ let thirty_two_bit_float: f32 = 7.90;
|
||||
let just_a_float = 69.69;
|
||||
```
|
||||
|
||||
|
||||
### Boolean
|
||||
|
||||
```rust {.wrap}
|
||||
@@ -166,7 +154,6 @@ let just_a_bool = true;
|
||||
let is_true = 8 < 5; // => false
|
||||
```
|
||||
|
||||
|
||||
### Character
|
||||
|
||||
```rust
|
||||
@@ -176,7 +163,6 @@ let implicit_char = '8';
|
||||
let emoji = "\u{1f600}"; // => 😀
|
||||
```
|
||||
|
||||
|
||||
### String Literal
|
||||
|
||||
```rust {.wrap}
|
||||
@@ -185,8 +171,8 @@ let no_of_members: &str = "ten";
|
||||
|
||||
println!("The name of the community is {community_name} and it has {no_of_members} members");
|
||||
```
|
||||
See: [Strings](#rust-strings)
|
||||
|
||||
See: [Strings](#rust-strings)
|
||||
|
||||
### Arrays
|
||||
|
||||
@@ -196,14 +182,13 @@ See: [Strings](#rust-strings)
|
||||
└─────┴─────┴─────┴─────┴─────┴─────┘
|
||||
0 1 2 3 4 5
|
||||
```
|
||||
----
|
||||
|
||||
---
|
||||
|
||||
```rust
|
||||
let array: [i64; 6] = [92,97,98,99,98,94];
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Multi-Dimensional Array {.row-span-2}
|
||||
|
||||
```rust
|
||||
@@ -214,7 +199,8 @@ i0 | 1 | 2 | 3 | 4 | 5 | 6 |
|
||||
i1 | 6 | 5 | 4 | 3 | 2 | 1 |
|
||||
└────┴────┴────┴────┴────┴────┘
|
||||
```
|
||||
----
|
||||
|
||||
---
|
||||
|
||||
```rust
|
||||
let array: [[i64; 6] ;2] = [
|
||||
@@ -230,10 +216,9 @@ let mut array: [i32 ; 3] = [2,6,10];
|
||||
array[1] = 4;
|
||||
array[2] = 6;
|
||||
```
|
||||
|
||||
Use the `mut` keyword to make it mutable.
|
||||
|
||||
|
||||
|
||||
### Slices
|
||||
|
||||
```rust
|
||||
@@ -243,14 +228,13 @@ let mut slices: &[i64] = &array[0..3] // Lower range is inclusive and upper rang
|
||||
println!("The elements of the slices are : {slices:?}");
|
||||
```
|
||||
|
||||
|
||||
### Vectors
|
||||
|
||||
```rust
|
||||
let some_vector = vec![1,2,3,4,5];
|
||||
let some_vector = vec![1,2,3,4,5];
|
||||
```
|
||||
A vector is declared using the `vec!` macro.
|
||||
|
||||
A vector is declared using the `vec!` macro.
|
||||
|
||||
### Tuples
|
||||
|
||||
@@ -258,10 +242,7 @@ A vector is declared using the `vec!` macro.
|
||||
let tuple = (1, 'A' , "Cool", 78, true);
|
||||
```
|
||||
|
||||
|
||||
|
||||
Rust Strings
|
||||
--------------
|
||||
## Rust Strings
|
||||
|
||||
### String Literal
|
||||
|
||||
@@ -272,7 +253,6 @@ let cs:&str = "cheat sheet";
|
||||
println!("Share {cs} for developers");
|
||||
```
|
||||
|
||||
|
||||
### String Object
|
||||
|
||||
```rust
|
||||
@@ -283,18 +263,18 @@ let my_string = String::new();
|
||||
let S_string = a_string.to_string()
|
||||
|
||||
// Creating an initialized string object
|
||||
let lang = String::from("Rust");
|
||||
let lang = String::from("Rust");
|
||||
println!("First language is {lang}");
|
||||
```
|
||||
```
|
||||
|
||||
### .capacity()
|
||||
|
||||
```rust
|
||||
```rust
|
||||
let rand = String::from("Random String");
|
||||
rand.capacity() // => 13
|
||||
```
|
||||
Calculates the capacity of the string in bytes.
|
||||
|
||||
Calculates the capacity of the string in bytes.
|
||||
|
||||
### .contains()
|
||||
|
||||
@@ -302,8 +282,8 @@ Calculates the capacity of the string in bytes.
|
||||
let name = String::from("ElementalX");
|
||||
name.contains("Element") // => true
|
||||
```
|
||||
Checks if the substring is contained inside the original string or not.
|
||||
|
||||
Checks if the substring is contained inside the original string or not.
|
||||
|
||||
### Pushing a single character
|
||||
|
||||
@@ -312,7 +292,6 @@ let mut half_text = String::from("Hal");
|
||||
half_text.push('f'); // => Half
|
||||
```
|
||||
|
||||
|
||||
### Pushing an entire String
|
||||
|
||||
```rust
|
||||
@@ -323,16 +302,12 @@ hi.push_str("How are you doing??");
|
||||
println!("{hi}");
|
||||
```
|
||||
|
||||
|
||||
|
||||
Rust Operators
|
||||
-----------
|
||||
|
||||
## Rust Operators
|
||||
|
||||
### Comparison Operators
|
||||
|
||||
| | |
|
||||
|----------|----------------------------------|
|
||||
| -------- | :------------------------------- |
|
||||
| `e == f` | `e` is equal to `f` |
|
||||
| `e != f` | `e` is NOT equal to `f` |
|
||||
| `e < f` | `e` is less than `f` |
|
||||
@@ -340,7 +315,7 @@ Rust Operators
|
||||
| `e <= f` | `e` is less than or equal to `f` |
|
||||
| `e >= f` | `e` is greater or equal to `f` |
|
||||
|
||||
---------
|
||||
---
|
||||
|
||||
```rust
|
||||
let (e, f) = (1, 100);
|
||||
@@ -353,18 +328,17 @@ let equal_to = e == f; // => false
|
||||
let not_equal_to = e != f; // => true
|
||||
```
|
||||
|
||||
|
||||
### Arithmetic Operators
|
||||
|
||||
| | |
|
||||
|----------|--------------------------------------------|
|
||||
| -------- | :----------------------------------------- |
|
||||
| `a + b` | `a` is added to `b` |
|
||||
| `a - b` | `b` is subtracted from `a` |
|
||||
| `a / b` | `a` is divided by `b` |
|
||||
| `a % b` | Gets remainder of `a` by dividing with `b` |
|
||||
| `a * b` | `a` is multiplied with `b` |
|
||||
|
||||
------
|
||||
---
|
||||
|
||||
```rust {.wrap}
|
||||
let (a, b) = (4, 5);
|
||||
@@ -376,21 +350,18 @@ let division: i32 = a / b; // => 0
|
||||
let modulus: i32 = a % b; // => 4
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### Bitwise Operators
|
||||
|
||||
| Operator | Description |
|
||||
|----------|-------------------------|
|
||||
| `g & h` | Binary AND |
|
||||
| `g | h` | Binary OR |
|
||||
| `g ^ h` | Binary XOR |
|
||||
| `!g` | Binary one's complement |
|
||||
| `g << h` | Binary shift left |
|
||||
| `g >> h` | Binary shift right |
|
||||
| Operator | Description |
|
||||
| ------------------- | ----------------------- |
|
||||
| `g & h` | Binary AND |
|
||||
| <code>g \| h</code> | Binary OR |
|
||||
| `g ^ h` | Binary XOR |
|
||||
| `!g` | Binary one's complement |
|
||||
| `g << h` | Binary shift left |
|
||||
| `g >> h` | Binary shift right |
|
||||
|
||||
-----
|
||||
---
|
||||
|
||||
```rust {.wrap}
|
||||
let (g, h) = (0x1, 0x2);
|
||||
@@ -399,20 +370,18 @@ let bitwise_and = g & h; // => 0
|
||||
let bitwise_or = g | h; // => 3
|
||||
let bitwise_xor = g ^ h; // => 3
|
||||
let right_shift = g >> 2; // => 0
|
||||
let left_shift = h << 4; // => 32
|
||||
let left_shift = h << 4; // => 32
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Logical Operators
|
||||
|
||||
| Example | Meaning |
|
||||
|----------------|------------------------|
|
||||
| `c && d` | Both are true _(AND)_ |
|
||||
| `c || d` | Either is true _(OR)_ |
|
||||
| `!c` | `c` is false _(NOT)_ |
|
||||
| Example | Meaning |
|
||||
| --------------------- | --------------------- |
|
||||
| `c && d` | Both are true _(AND)_ |
|
||||
| <code>c \|\| d</code> | Either is true _(OR)_ |
|
||||
| `!c` | `c` is false _(NOT)_ |
|
||||
|
||||
------
|
||||
---
|
||||
|
||||
```rust
|
||||
let (c, d) = (true, false);
|
||||
@@ -422,7 +391,6 @@ let or = c || d; // => true
|
||||
let not = !c; // => false
|
||||
```
|
||||
|
||||
|
||||
### Compound Assignment Operator
|
||||
|
||||
```rust
|
||||
@@ -430,22 +398,17 @@ let mut k = 9;
|
||||
let mut l = k;
|
||||
```
|
||||
|
||||
----
|
||||
---
|
||||
|
||||
| Operator | Description |
|
||||
|-----------|-----------------------------------------|
|
||||
| `k += l` | Add a value and assign, then k=9 |
|
||||
| `k -= l` | Substrate a value and assign, then k=18 |
|
||||
| `k /= l` | Divide a value and assign, then k=9 |
|
||||
| `k *= l` | Multiply a value and assign, then k=81 |
|
||||
| `k |= l` | Bitwise OR and assign, then k=89 |
|
||||
| Operator | Description |
|
||||
| -------------------- | --------------------------------------- |
|
||||
| `k += l` | Add a value and assign, then k=9 |
|
||||
| `k -= l` | Substrate a value and assign, then k=18 |
|
||||
| `k /= l` | Divide a value and assign, then k=9 |
|
||||
| `k *= l` | Multiply a value and assign, then k=81 |
|
||||
| <code>k \|= l</code> | Bitwise OR and assign, then k=89 |
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Rust Flow Control
|
||||
--------------
|
||||
## Rust Flow Control
|
||||
|
||||
### If Expression
|
||||
|
||||
@@ -471,8 +434,6 @@ if case3 >= case4 {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### If...Else...if...Else Expression
|
||||
|
||||
```rust
|
||||
@@ -490,7 +451,6 @@ if foo == bar {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### If...Let Expression {.row-span-3}
|
||||
|
||||
```rust
|
||||
@@ -504,7 +464,9 @@ if let["Apple", _] = arr2{
|
||||
println!("Works with str array too");
|
||||
}
|
||||
```
|
||||
----
|
||||
|
||||
---
|
||||
|
||||
```rust
|
||||
let tuple_1 = ("India", 7, 90, 90.432);
|
||||
if let(_, 7, 9, 78.99) = tuple_1{
|
||||
@@ -555,8 +517,7 @@ match day_of_week {
|
||||
println!("Default!")
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
### Nested...If Expression
|
||||
|
||||
@@ -568,8 +529,7 @@ if nested_conditions == 89 {
|
||||
println!("Greater than 97");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
### For Loop
|
||||
|
||||
@@ -580,8 +540,6 @@ for mut i in 0..15 {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### While Loop
|
||||
|
||||
```rust
|
||||
@@ -597,7 +555,6 @@ while check < 11{
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Loop keyword
|
||||
|
||||
```rust
|
||||
@@ -605,6 +562,7 @@ loop {
|
||||
println!("hello world forever!");
|
||||
}
|
||||
```
|
||||
|
||||
The infinite loop indicated.
|
||||
|
||||
### Break Statement
|
||||
@@ -618,8 +576,7 @@ loop {
|
||||
}
|
||||
i *= 2;
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
|
||||
### Continue Statement
|
||||
|
||||
@@ -634,11 +591,7 @@ for (v, c) in (0..10+1).enumerate(){
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
Rust Functions
|
||||
--------------
|
||||
|
||||
## Rust Functions
|
||||
|
||||
### Basic function
|
||||
|
||||
@@ -661,7 +614,7 @@ fn main()
|
||||
{
|
||||
let x:u32 = 10;
|
||||
let y:u32 = 20;
|
||||
|
||||
|
||||
// => 200
|
||||
println!("Calc: {}", cal_rect(x, y));
|
||||
}
|
||||
@@ -687,9 +640,7 @@ fn power_of_three(by_ref: &mut i32){
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Returns
|
||||
### Returns
|
||||
|
||||
```rust {.wrap}
|
||||
fn main(){
|
||||
@@ -746,12 +697,7 @@ fn multiply (mut arr: [i32 ; 5]) -> [i32 ; 5]{
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
Misc
|
||||
--------------
|
||||
|
||||
## Misc
|
||||
|
||||
### Type Casting
|
||||
|
||||
@@ -760,18 +706,17 @@ let a_int = 90; // int
|
||||
// int to float
|
||||
let mut type_cast = (a_int as f64);
|
||||
```
|
||||
------
|
||||
|
||||
---
|
||||
|
||||
```rust
|
||||
let orginal: char = 'I';
|
||||
let original: char = 'I';
|
||||
// char to int => 73
|
||||
let type_casted: i64 = orginal as i64;
|
||||
let type_casted: i64 = original as i64;
|
||||
```
|
||||
|
||||
To perform type-casting in Rust one must use the `as` keyword.
|
||||
|
||||
|
||||
|
||||
|
||||
### Borrowing
|
||||
|
||||
```rust
|
||||
@@ -779,7 +724,9 @@ let mut foo = 4;
|
||||
let mut borrowed_foo = &foo;
|
||||
println!("{borrowed_foo}");
|
||||
```
|
||||
------
|
||||
|
||||
---
|
||||
|
||||
```rust
|
||||
let mut bar = 3;
|
||||
let mut mutable_borrowed_bar = &mut bar;
|
||||
@@ -788,8 +735,6 @@ println!("{mutable_borrowed_bar}");
|
||||
|
||||
Here borrowed value borrows the value from value one using `&` operator.
|
||||
|
||||
|
||||
|
||||
### De-referencing
|
||||
|
||||
```rust
|
||||
@@ -801,8 +746,8 @@ println!("{}", *deref);
|
||||
|
||||
De-referencing in rust can be done using the `*` operator
|
||||
|
||||
|
||||
### Variable Scope
|
||||
|
||||
```rust
|
||||
{
|
||||
// The scope limited to this braces
|
||||
@@ -810,11 +755,10 @@ De-referencing in rust can be done using the `*` operator
|
||||
}
|
||||
println!("{a_number}");
|
||||
```
|
||||
|
||||
This will produce error as the scope of the variable `a_number` ends at the braces
|
||||
|
||||
|
||||
Also see
|
||||
--------
|
||||
## Also see
|
||||
|
||||
- [The Rust Document](https://doc.rust-lang.org/book/ch00-00-introduction.html) _(doc.rust-lang.org)_
|
||||
- [The Rust Reference](https://doc.rust-lang.org/reference/introduction.html) _(doc.rust-lang.org)_
|
||||
|
||||
Reference in new issue
Block a user