2022-11-16 14:10:09 +08:00
|
|
|
---
|
|
|
|
|
title: Rust
|
|
|
|
|
date: 2022-01-01 11:51:44
|
|
|
|
|
background: bg-black
|
|
|
|
|
tags:
|
|
|
|
|
categories:
|
2024-05-22 02:51:48 -07:00
|
|
|
- Programming
|
2022-11-16 14:10:09 +08:00
|
|
|
intro: |
|
2024-05-22 02:51:48 -07:00
|
|
|
The Rust quick reference cheat sheet that aims at providing help on writing basic syntax and methods.
|
2023-03-06 16:19:19 +08:00
|
|
|
plugins:
|
2024-05-22 02:51:48 -07:00
|
|
|
- copyCode
|
2022-11-16 14:10:09 +08:00
|
|
|
---
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Getting Started
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Hello_World.rs
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
fn main() {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("Hello, World!");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
2024-05-22 02:51:48 -07:00
|
|
|
|
2022-11-16 14:10:09 +08:00
|
|
|
#### Compiling and Running
|
2024-05-22 02:51:48 -07:00
|
|
|
|
2022-11-16 14:10:09 +08:00
|
|
|
```shell
|
|
|
|
|
$ rustc Hello_World.rs
|
|
|
|
|
$ ./Hello_World
|
|
|
|
|
Hello, World!
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Primitive types
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
| | |
|
|
|
|
|
| ------------------------- | :------------------------------ |
|
|
|
|
|
| `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 |
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
See: [Rust Types](#rust-types)
|
|
|
|
|
|
|
|
|
|
### Formatting {.row-span-2}
|
|
|
|
|
|
|
|
|
|
```rust {.wrap}
|
|
|
|
|
// Single Placeholder
|
|
|
|
|
println!("{}", 1);
|
|
|
|
|
|
|
|
|
|
// Multiple Placeholder
|
|
|
|
|
println!("{} {}", 1, 3);
|
|
|
|
|
|
|
|
|
|
// Positional Arguments
|
2024-08-13 05:08:48 +09:00
|
|
|
println!(
|
2025-09-01 10:15:50 +02:00
|
|
|
"{0} is a {1} {2}, also {0} is a {3} programming language",
|
2024-08-13 05:08:48 +09:00
|
|
|
"Rust", "cool", "language", "safe"
|
|
|
|
|
);
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
// Named Arguments
|
2024-08-13 05:08:48 +09:00
|
|
|
println!(
|
|
|
|
|
"{country} is a diverse nation with unity.",
|
|
|
|
|
country = "India"
|
|
|
|
|
);
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
// Placeholder traits :b for binary, :0x is for hex and :o is octal
|
|
|
|
|
println!("Let us print 76 is binary which is {:b} , and hex equivalent is {:0x} and octal equivalent is {:o}", 76, 76, 76);
|
|
|
|
|
|
|
|
|
|
// Debug Trait
|
2024-08-13 05:08:48 +09:00
|
|
|
println!(
|
|
|
|
|
"Print whatever we want to here using debug trait {:?}",
|
|
|
|
|
(76, 'A', 90)
|
|
|
|
|
);
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
// New Format Strings in 1.58
|
|
|
|
|
let x = "world";
|
|
|
|
|
println!("Hello {x}!");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Printing Styles
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
// Prints the output
|
|
|
|
|
print!("Hello World\n");
|
|
|
|
|
|
|
|
|
|
// Appends a new line after printing
|
|
|
|
|
println!("Appending a new line");
|
|
|
|
|
|
|
|
|
|
// Prints as an error
|
|
|
|
|
eprint!("This is an error\n");
|
|
|
|
|
|
|
|
|
|
// Prints as an error with new line
|
|
|
|
|
eprintln!("This is an error with new line");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Variables
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
// Initializing and declaring a variable
|
|
|
|
|
let some_variable = "This_is_a_variable";
|
|
|
|
|
|
|
|
|
|
// Making a variable mutable
|
|
|
|
|
let mut mutable_variable = "Mutable";
|
|
|
|
|
|
|
|
|
|
// Assigning multiple variables
|
|
|
|
|
let (name, age) = ("ElementalX", 20);
|
|
|
|
|
|
|
|
|
|
// (Global) constant
|
2024-08-13 05:08:48 +09:00
|
|
|
const SCREAMING_SNAKE_CASE: i64 = 9;
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Comments
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
// Line Comments
|
|
|
|
|
/*.............Block Comments */
|
|
|
|
|
/// Outer doc comments
|
|
|
|
|
//! Inner doc comments
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
See: [Comment](https://doc.rust-lang.org/reference/comments.html)
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Functions
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
fn test() {
|
|
|
|
|
println!("This is a function!");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
fn main() {
|
|
|
|
|
test();
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
See: [Functions](#rust-functions)
|
2022-11-16 14:10:09 +08:00
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Rust Types
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Integer
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut a: u32 = 8;
|
|
|
|
|
let b: u64 = 877;
|
|
|
|
|
let c: i64 = 8999;
|
|
|
|
|
let d = -90;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Floating-Point
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut sixty_bit_float: f64 = 89.90;
|
|
|
|
|
let thirty_two_bit_float: f32 = 7.90;
|
|
|
|
|
let just_a_float = 69.69;
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Boolean
|
|
|
|
|
|
|
|
|
|
```rust {.wrap}
|
|
|
|
|
let true_val: bool = true;
|
|
|
|
|
let false_val: bool = false;
|
|
|
|
|
let just_a_bool = true;
|
2024-08-13 05:08:48 +09:00
|
|
|
let is_true = 8 < 5; // => false
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Character
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let first_letter_of_alphabet = 'a';
|
|
|
|
|
let explicit_char: char = 'F';
|
|
|
|
|
let implicit_char = '8';
|
2024-08-13 05:08:48 +09:00
|
|
|
let emoji = "\u{1f600}"; // => 😀
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### String Literal
|
|
|
|
|
|
|
|
|
|
```rust {.wrap}
|
|
|
|
|
let community_name = "AXIAL";
|
|
|
|
|
let no_of_members: &str = "ten";
|
|
|
|
|
|
|
|
|
|
println!("The name of the community is {community_name} and it has {no_of_members} members");
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
See: [Strings](#rust-strings)
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Arrays
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
┌─────┬─────┬─────┬─────┬─────┬─────┐
|
|
|
|
|
| 92 | 97 | 98 | 99 | 98 | 94 |
|
|
|
|
|
└─────┴─────┴─────┴─────┴─────┴─────┘
|
|
|
|
|
0 1 2 3 4 5
|
|
|
|
|
```
|
2024-05-22 02:51:48 -07:00
|
|
|
|
|
|
|
|
---
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let array: [i64; 6] = [92, 97, 98, 99, 98, 94];
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Multi-Dimensional Array {.row-span-2}
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
j0 j1 j2 j3 j4 j5
|
|
|
|
|
┌────┬────┬────┬────┬────┬────┐
|
|
|
|
|
i0 | 1 | 2 | 3 | 4 | 5 | 6 |
|
|
|
|
|
├────┼────┼────┼────┼────┼────┤
|
|
|
|
|
i1 | 6 | 5 | 4 | 3 | 2 | 1 |
|
|
|
|
|
└────┴────┴────┴────┴────┴────┘
|
|
|
|
|
```
|
2024-05-22 02:51:48 -07:00
|
|
|
|
|
|
|
|
---
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let array: [[i64; 6]; 2] = [
|
|
|
|
|
[1, 2, 3, 4, 5, 6],
|
|
|
|
|
[6, 5, 4, 3, 2, 1]
|
|
|
|
|
];
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Mutable Array
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let mut array: [i32; 3] = [2, 6, 10];
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
array[1] = 4;
|
|
|
|
|
array[2] = 6;
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
Use the `mut` keyword to make it mutable.
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Slices
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let mut array: [i64; 4] = [1, 2, 3, 4];
|
|
|
|
|
let mut slices: &[i64] = &array[0..3]; // Lower range is inclusive and upper range is exclusive
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
println!("The elements of the slices are : {slices:?}");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Vectors
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let some_vector = vec![1, 2, 3, 4, 5];
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
A vector is declared using the `vec!` macro.
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Tuples
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let tuple = (1, 'A', "Cool", 78, true);
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Rust Strings
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### String Literal
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let cs: &str = "cheat sheet";
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
// => Share cheat sheet for developers
|
|
|
|
|
println!("Share {cs} for developers");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### String Object
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
// Creating an empty string object
|
2024-05-04 08:52:36 +05:30
|
|
|
let my_string = String::new();
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
// Converting to a string object
|
2025-09-01 10:22:07 +02:00
|
|
|
let S_string = a_string.to_string();
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
// Creating an initialized string object
|
2024-05-22 02:51:48 -07:00
|
|
|
let lang = String::from("Rust");
|
2022-11-16 14:10:09 +08:00
|
|
|
println!("First language is {lang}");
|
2024-05-22 02:51:48 -07:00
|
|
|
```
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### .capacity()
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
```rust
|
2022-11-16 14:10:09 +08:00
|
|
|
let rand = String::from("Random String");
|
2024-08-13 05:08:48 +09:00
|
|
|
rand.capacity() // => 13
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
Calculates the capacity of the string in bytes.
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### .contains()
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let name = String::from("ElementalX");
|
|
|
|
|
name.contains("Element") // => true
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
Checks if the substring is contained inside the original string or not.
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Pushing a single character
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut half_text = String::from("Hal");
|
2024-08-13 05:08:48 +09:00
|
|
|
half_text.push('f'); // => Half
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Pushing an entire String
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut hi = String::from("Hey there...");
|
|
|
|
|
hi.push_str("How are you doing??");
|
|
|
|
|
|
|
|
|
|
// => Hey there...How are you doing??
|
|
|
|
|
println!("{hi}");
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Rust Operators
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Comparison Operators
|
|
|
|
|
|
|
|
|
|
| | |
|
2024-05-22 02:51:48 -07:00
|
|
|
| -------- | :------------------------------- |
|
2022-11-16 14:10:09 +08:00
|
|
|
| `e == f` | `e` is equal to `f` |
|
|
|
|
|
| `e != f` | `e` is NOT equal to `f` |
|
|
|
|
|
| `e < f` | `e` is less than `f` |
|
2025-09-01 10:51:20 +02:00
|
|
|
| `e > f` | `e` is greater than `f` |
|
2022-11-16 14:10:09 +08:00
|
|
|
| `e <= f` | `e` is less than or equal to `f` |
|
|
|
|
|
| `e >= f` | `e` is greater or equal to `f` |
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
---
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let (e, f) = (1, 100);
|
|
|
|
|
|
|
|
|
|
let greater = f > e; // => true
|
|
|
|
|
let less = f < e; // => false
|
|
|
|
|
let greater_equal = f >= e; // => true
|
|
|
|
|
let less_equal = e <= f; // => true
|
|
|
|
|
let equal_to = e == f; // => false
|
|
|
|
|
let not_equal_to = e != f; // => true
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Arithmetic Operators
|
|
|
|
|
|
|
|
|
|
| | |
|
2024-05-22 02:51:48 -07:00
|
|
|
| -------- | :----------------------------------------- |
|
2022-11-16 14:10:09 +08:00
|
|
|
| `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` |
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
---
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
```rust {.wrap}
|
|
|
|
|
let (a, b) = (4, 5);
|
|
|
|
|
|
|
|
|
|
let sum: i32 = a + b; // => 9
|
|
|
|
|
let subtractions: i32 = a - b; // => -1
|
|
|
|
|
let multiplication: i32 = a * b; // => 20
|
|
|
|
|
let division: i32 = a / b; // => 0
|
|
|
|
|
let modulus: i32 = a % b; // => 4
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Bitwise Operators
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
| 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 |
|
2022-11-16 14:10:09 +08:00
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
---
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
```rust {.wrap}
|
|
|
|
|
let (g, h) = (0x1, 0x2);
|
|
|
|
|
|
|
|
|
|
let bitwise_and = g & h; // => 0
|
|
|
|
|
let bitwise_or = g | h; // => 3
|
|
|
|
|
let bitwise_xor = g ^ h; // => 3
|
|
|
|
|
let right_shift = g >> 2; // => 0
|
2024-05-22 02:51:48 -07:00
|
|
|
let left_shift = h << 4; // => 32
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Logical Operators
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
| Example | Meaning |
|
|
|
|
|
| --------------------- | --------------------- |
|
|
|
|
|
| `c && d` | Both are true _(AND)_ |
|
|
|
|
|
| <code>c \|\| d</code> | Either is true _(OR)_ |
|
|
|
|
|
| `!c` | `c` is false _(NOT)_ |
|
2022-11-16 14:10:09 +08:00
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
---
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let (c, d) = (true, false);
|
|
|
|
|
|
|
|
|
|
let and = c && d; // => false
|
2024-08-13 05:08:48 +09:00
|
|
|
let or = c || d; // => true
|
2022-11-16 14:10:09 +08:00
|
|
|
let not = !c; // => false
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Compound Assignment Operator
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut k = 9;
|
|
|
|
|
let mut l = k;
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
---
|
2022-11-16 14:10:09 +08:00
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
| Operator | Description |
|
|
|
|
|
| -------------------- | --------------------------------------- |
|
2025-09-01 10:56:39 +02:00
|
|
|
| `k += l` | Add a value and assign, then k=18 |
|
|
|
|
|
| `k -= l` | Subtract a value and assign, then k=0 |
|
|
|
|
|
| `k /= l` | Divide a value and assign, then k=1 |
|
2024-05-22 02:51:48 -07:00
|
|
|
| `k *= l` | Multiply a value and assign, then k=81 |
|
|
|
|
|
| <code>k \|= l</code> | Bitwise OR and assign, then k=89 |
|
2022-11-16 14:10:09 +08:00
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Rust Flow Control
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### If Expression
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let case1: i32 = 81;
|
|
|
|
|
let case2: i32 = 82;
|
|
|
|
|
|
|
|
|
|
if case1 < case2 {
|
2025-09-01 10:27:25 +02:00
|
|
|
println!("case1 is less than case2");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### If...Else Expression
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let case3 = 8;
|
|
|
|
|
let case4 = 9;
|
|
|
|
|
|
|
|
|
|
if case3 >= case4 {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("case3 is better than case4");
|
2022-11-16 14:10:09 +08:00
|
|
|
} else {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("case4 is greater than case3");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### If...Else...if...Else Expression
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let foo = 12;
|
|
|
|
|
let bar = 13;
|
|
|
|
|
|
|
|
|
|
if foo == bar {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("foo is equal to bar");
|
2022-11-16 14:10:09 +08:00
|
|
|
} else if foo < bar {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("foo less than bar");
|
2022-11-16 14:10:09 +08:00
|
|
|
} else if foo != bar {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("foo is not equal to bar");
|
2022-11-16 14:10:09 +08:00
|
|
|
} else {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("Nothing");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### If...Let Expression {.row-span-3}
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let mut arr1: [i64; 3] = [1, 2, 3];
|
|
|
|
|
if let [1, 2, _] = arr1 {
|
2022-11-16 14:10:09 +08:00
|
|
|
println!("Works with array");
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
let mut arr2: [&str; 2] = ["one", "two"];
|
|
|
|
|
if let ["Apple", _] = arr2 {
|
2022-11-16 14:10:09 +08:00
|
|
|
println!("Works with str array too");
|
|
|
|
|
}
|
|
|
|
|
```
|
2024-05-22 02:51:48 -07:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2022-11-16 14:10:09 +08:00
|
|
|
```rust
|
|
|
|
|
let tuple_1 = ("India", 7, 90, 90.432);
|
2024-08-13 05:08:48 +09:00
|
|
|
if let (_, 7, 9, 78.99) = tuple_1 {
|
2022-11-16 14:10:09 +08:00
|
|
|
println!("Works with tuples too");
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
let tuple_2 = (9, 7, 89, 12, "Okay");
|
|
|
|
|
if let (9, 7, 89, 12, blank) = tuple_2 {
|
2022-11-16 14:10:09 +08:00
|
|
|
println!("Everything {blank} mate?");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let tuple_3 = (89, 90, "Yes");
|
2024-08-13 05:08:48 +09:00
|
|
|
if let (9, 89, "Yes") = tuple_3 {
|
2022-11-16 14:10:09 +08:00
|
|
|
println!("Pattern did match");
|
2024-08-13 05:08:48 +09:00
|
|
|
} else {
|
2022-11-16 14:10:09 +08:00
|
|
|
println!("Pattern did not match");
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Match Expression {.row-span-3}
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let day_of_week = 2;
|
2023-08-29 11:00:06 +08:00
|
|
|
match day_of_week {
|
2024-08-13 05:08:48 +09:00
|
|
|
1 => {
|
|
|
|
|
println!("Its Monday my dudes");
|
|
|
|
|
}
|
|
|
|
|
2 => {
|
|
|
|
|
println!("It's Tuesday my dudes");
|
|
|
|
|
}
|
|
|
|
|
3 => {
|
|
|
|
|
println!("It's Wednesday my dudes");
|
|
|
|
|
}
|
|
|
|
|
4 => {
|
|
|
|
|
println!("It's Thursday my dudes");
|
|
|
|
|
}
|
|
|
|
|
5 => {
|
|
|
|
|
println!("It's Friday my dudes");
|
|
|
|
|
}
|
|
|
|
|
6 => {
|
|
|
|
|
println!("It's Saturday my dudes");
|
|
|
|
|
}
|
|
|
|
|
7 => {
|
|
|
|
|
println!("It's Sunday my dudes");
|
|
|
|
|
}
|
|
|
|
|
_ => {
|
|
|
|
|
println!("Default!")
|
|
|
|
|
}
|
2023-08-29 11:00:06 +08:00
|
|
|
};
|
2024-05-22 02:51:48 -07:00
|
|
|
```
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Nested...If Expression
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let nested_conditions = 89;
|
|
|
|
|
if nested_conditions == 89 {
|
|
|
|
|
let just_a_value = 98;
|
|
|
|
|
if just_a_value >= 97 {
|
|
|
|
|
println!("Greater than 97");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-22 02:51:48 -07:00
|
|
|
```
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### For Loop
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
for mut i in 0..15 {
|
2024-08-13 05:08:48 +09:00
|
|
|
i -= 1;
|
|
|
|
|
println!("The value of i is : {i}");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### While Loop
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
let mut check = 0;
|
|
|
|
|
while check < 11 {
|
|
|
|
|
println!("Check is : {check}");
|
|
|
|
|
check += 1;
|
|
|
|
|
println!("After incrementing: {check}");
|
2022-11-16 14:10:09 +08:00
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
if check == 10 {
|
|
|
|
|
break; // stop while
|
|
|
|
|
}
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Loop keyword
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
loop {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("hello world forever!");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
2024-05-22 02:51:48 -07:00
|
|
|
|
2022-11-16 14:10:09 +08:00
|
|
|
The infinite loop indicated.
|
|
|
|
|
|
|
|
|
|
### Break Statement
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut i = 1;
|
|
|
|
|
loop {
|
2024-08-13 05:08:48 +09:00
|
|
|
println!("i is {i}");
|
|
|
|
|
if i > 100 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
i *= 2;
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
2024-05-22 02:51:48 -07:00
|
|
|
```
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Continue Statement
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
for (v, c) in (0..10 + 1).enumerate() {
|
|
|
|
|
println!("The {c} number loop");
|
|
|
|
|
if v == 9 {
|
|
|
|
|
println!("Here we go continue?");
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2025-09-01 10:52:26 +02:00
|
|
|
println!("The value of v is : {v}");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Rust Functions
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Basic function
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
fn print_message() {
|
|
|
|
|
println!("Hello, CheatSheets.zip!");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
fn main() {
|
|
|
|
|
//Invoking a function in Rust.
|
|
|
|
|
print_message();
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Pass by Value
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
fn main() {
|
|
|
|
|
let x: u32 = 10;
|
|
|
|
|
let y: u32 = 20;
|
2024-05-22 02:51:48 -07:00
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
// => 200
|
|
|
|
|
println!("Calc: {}", cal_rect(x, y));
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
2024-08-13 05:08:48 +09:00
|
|
|
|
|
|
|
|
fn cal_rect(x: u32, y: u32) -> u32 {
|
|
|
|
|
x * y
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Pass by Reference
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
fn main() {
|
|
|
|
|
let mut by_ref = 3; // => 3
|
|
|
|
|
power_of_three(&mut by_ref);
|
|
|
|
|
println!("{by_ref}"); // => 9
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
fn power_of_three(by_ref: &mut i32) {
|
|
|
|
|
// de-referencing is important
|
|
|
|
|
*by_ref = *by_ref * *by_ref;
|
|
|
|
|
println!("{by_ref}"); // => 9
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
### Returns
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
```rust {.wrap}
|
2024-08-13 05:08:48 +09:00
|
|
|
fn main() {
|
|
|
|
|
let (mut radius, mut pi) = (3.0, 3.14);
|
|
|
|
|
let (area, _perimeter) = calculate(
|
|
|
|
|
&mut radius,
|
|
|
|
|
&mut pi
|
|
|
|
|
);
|
|
|
|
|
println!("The area and the perimeter of the circle are: {area} & {_perimeter}");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
fn calculate(radius: &mut f64, pi: &mut f64) -> (f64, f64) {
|
|
|
|
|
let perimeter = 2.0 * *pi * *radius;
|
|
|
|
|
let area = *pi * *radius * *radius;
|
|
|
|
|
return (area, perimeter);
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Arrays as Arguments
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
fn main() {
|
|
|
|
|
let mut array: [i32; 5] = [1, 2, 3, 4, 6];
|
|
|
|
|
print_arrays(array);
|
|
|
|
|
println!("The elements: {array:?}");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
fn print_arrays(mut array: [i32; 5]) {
|
|
|
|
|
array[0] = 89;
|
|
|
|
|
array[1] = 90;
|
|
|
|
|
array[2] = 91;
|
|
|
|
|
array[3] = 92;
|
|
|
|
|
array[4] = 93;
|
|
|
|
|
println!("The elements: {array:?}");
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Returning Arrays
|
|
|
|
|
|
|
|
|
|
```rust
|
2024-08-13 05:08:48 +09:00
|
|
|
fn main() {
|
|
|
|
|
let mut arr: [i32; 5] = [2, 4, 6, 8, 10];
|
|
|
|
|
multiply(arr);
|
|
|
|
|
println!("The array is : {:?}", multiply(arr));
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-13 05:08:48 +09:00
|
|
|
fn multiply(mut arr: [i32; 5]) -> [i32; 5] {
|
|
|
|
|
arr[2] = 90;
|
|
|
|
|
for mut i in 0..5 {
|
|
|
|
|
arr[i] = arr[i] * arr[2];
|
|
|
|
|
}
|
|
|
|
|
return arr;
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Misc
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
### Type Casting
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let a_int = 90; // int
|
|
|
|
|
// int to float
|
|
|
|
|
let mut type_cast = (a_int as f64);
|
|
|
|
|
```
|
2024-05-22 02:51:48 -07:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2022-11-16 14:10:09 +08:00
|
|
|
```rust
|
2024-05-22 02:51:48 -07:00
|
|
|
let original: char = 'I';
|
2022-11-16 14:10:09 +08:00
|
|
|
// char to int => 73
|
2024-05-22 02:51:48 -07:00
|
|
|
let type_casted: i64 = original as i64;
|
2022-11-16 14:10:09 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
To perform type-casting in Rust one must use the `as` keyword.
|
|
|
|
|
|
|
|
|
|
### Borrowing
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut foo = 4;
|
|
|
|
|
let mut borrowed_foo = &foo;
|
|
|
|
|
println!("{borrowed_foo}");
|
|
|
|
|
```
|
2024-05-22 02:51:48 -07:00
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
2022-11-16 14:10:09 +08:00
|
|
|
```rust
|
|
|
|
|
let mut bar = 3;
|
|
|
|
|
let mut mutable_borrowed_bar = &mut bar;
|
|
|
|
|
println!("{mutable_borrowed_bar}");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
Here borrowed value borrows the value from value one using `&` operator.
|
|
|
|
|
|
|
|
|
|
### De-referencing
|
|
|
|
|
|
|
|
|
|
```rust
|
|
|
|
|
let mut borrow = 10;
|
|
|
|
|
let deref = &mut borrow;
|
|
|
|
|
|
|
|
|
|
println!("{}", *deref);
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
De-referencing in rust can be done using the `*` operator
|
|
|
|
|
|
|
|
|
|
### Variable Scope
|
2024-05-22 02:51:48 -07:00
|
|
|
|
2022-11-16 14:10:09 +08:00
|
|
|
```rust
|
|
|
|
|
{
|
2024-08-13 05:08:48 +09:00
|
|
|
// The scope limited to this braces
|
|
|
|
|
let a_number = 1;
|
2022-11-16 14:10:09 +08:00
|
|
|
}
|
|
|
|
|
println!("{a_number}");
|
|
|
|
|
```
|
|
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
This will produce error as the scope of the variable `a_number` ends at the braces
|
2022-11-16 14:10:09 +08:00
|
|
|
|
2024-05-22 02:51:48 -07:00
|
|
|
## Also see
|
2022-11-16 14:10:09 +08:00
|
|
|
|
|
|
|
|
- [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)_
|
|
|
|
|
- [Rust Cheatsheet](https://phaiax.github.io/rust-cheatsheet/) _(phaiax.github.io)_
|