From b7d02f7a3b71e950ca79ac8e9f1a6916ae20be68 Mon Sep 17 00:00:00 2001 From: Dmitry Voronin Date: Sat, 18 Nov 2023 09:54:14 +0300 Subject: [PATCH] rust : wip. --- dev/lang/rust/array.md | 32 ++++++++++++++++++++++++++++++++ dev/lang/rust/borrow.md | 37 +++++++++++++++++++++++++++++++++++++ dev/lang/rust/function.md | 26 ++++++++++++++++++++++++++ dev/lang/rust/if.md | 24 ++++++++++++++++++++++++ dev/lang/rust/primitive.md | 15 +++++++++++++++ dev/lang/rust/tuple.md | 20 ++++++++++++++++++++ dev/lang/rust/vector.md | 25 +++++++++++++++++++++++++ 7 files changed, 179 insertions(+) create mode 100644 dev/lang/rust/array.md create mode 100644 dev/lang/rust/borrow.md create mode 100644 dev/lang/rust/if.md create mode 100644 dev/lang/rust/primitive.md create mode 100644 dev/lang/rust/tuple.md create mode 100644 dev/lang/rust/vector.md diff --git a/dev/lang/rust/array.md b/dev/lang/rust/array.md new file mode 100644 index 0000000..0c0da8c --- /dev/null +++ b/dev/lang/rust/array.md @@ -0,0 +1,32 @@ +# Arrays. + +# Create an array. + +Specify the type first, then the count. + +```rust +let array: [i32; 5] = [1, 2, 3, 4, 5]; // Manual init. +let array: [i32; 5] = [0; 5]; // All to zeroes. +``` + +# Get the array size. + +Use `.len()` method. + +```rust +let size = array.len(); +``` + +# Create a slice. + +## Whole array. + +```rust +let slice = &array; +``` + +## Partial. + +```rust +let slice = &array[1..4]; // Slice of: 1, 2, 3. +``` diff --git a/dev/lang/rust/borrow.md b/dev/lang/rust/borrow.md new file mode 100644 index 0000000..30aab90 --- /dev/null +++ b/dev/lang/rust/borrow.md @@ -0,0 +1,37 @@ +# Memory borrowing. + +# Reference. + +To pass an immutable reference, use `&` sign. + +```rust +let s1 = String::from("hello"); +foo(&s1); +``` + +# Mutable reference. + +Use `&mut` to pass mutable reference. You can have only one mutable reference at a time. + +```rust +let s1 = String::from("world"); +foo(&mut s1) +``` + +# Ownership. + +You pass ownership by default: no special syntax. + +```rust +let s1 = String::from("hi"); +foo(s1) // foo() now owns s1 and s1 is no longer accesible after this call. +``` + +```rust +fn foo() -> String +{ + let s1 = String::from("wow"); + + return s1; +} +``` diff --git a/dev/lang/rust/function.md b/dev/lang/rust/function.md index eb9d9d1..f238aa5 100644 --- a/dev/lang/rust/function.md +++ b/dev/lang/rust/function.md @@ -5,7 +5,33 @@ Declare functions using `fn` keyword. ```rust +// Simple function. fn main() {} +// With argument of type i32. fn main(x: i32) {} + +// With return type of i32. +fn main() -> i32 {} +``` + +# Return value. + +## Return keyword. + +You can return value by writing `return` keyword. Use this in multiline functions. + +```rust +fn foo() -> i32 +{ + return 10; +} +``` + +## Last expression. + +It is also possible to return the result of the last line by removing `;` at the end. Use this for single-line functions. + +```rust +fn foo() -> i32 { 10 } ``` diff --git a/dev/lang/rust/if.md b/dev/lang/rust/if.md new file mode 100644 index 0000000..2d285bd --- /dev/null +++ b/dev/lang/rust/if.md @@ -0,0 +1,24 @@ +# If statement. + +# Simple switch + +```rust +if (x == 10) +{ } +else +{ } +``` + +# As an expression. + +```rust +fn foo(a: i32, b: i32) -> i32 { if (a > b) { a } else { b } } +``` + +# Variants. + +Using `()` is optional, however, `{}` are required. + +```rust +if a > b { a } else { b } +``` diff --git a/dev/lang/rust/primitive.md b/dev/lang/rust/primitive.md new file mode 100644 index 0000000..cf35647 --- /dev/null +++ b/dev/lang/rust/primitive.md @@ -0,0 +1,15 @@ +# Primitive types. + +# Boolean. + +```rust +let foo: bool = true; +``` + +# Char. + +Char is identified by using single quotes `'c'`. + +```rust +let foo: char = 'c'; +``` diff --git a/dev/lang/rust/tuple.md b/dev/lang/rust/tuple.md new file mode 100644 index 0000000..ca379c3 --- /dev/null +++ b/dev/lang/rust/tuple.md @@ -0,0 +1,20 @@ +# Tuple. + +# Create. + +```rust +let cat = ("Fluff", 3.5); +``` + +# Destructure. + +```rust +let (name, age) = cat; +``` + +# Indexed access. + +```rust +let numbers = (1, 2, 3); +let two = numbers.1; +``` diff --git a/dev/lang/rust/vector.md b/dev/lang/rust/vector.md new file mode 100644 index 0000000..86cb9f4 --- /dev/null +++ b/dev/lang/rust/vector.md @@ -0,0 +1,25 @@ +# Vector. + +# Create. + +## Using macro. + +```rust +let array: [i32; 4] = [10, 20, 30, 40]; +let vector: Vec = vec!(10, 20, 30, 40); +``` + +# Iterate. + +## For-each. + +```rust +for (item in vector.iter()) {} // Immutable iterate. +for (item in vector.iter_mut()) {} // Mutable iterate. +``` + +## Map. + +```rust +vector.iter().map(|element| {}).collect() +```