diff --git a/dev/lang/rust/enum.md b/dev/lang/rust/enum.md index 93e1e86..99f1961 100644 --- a/dev/lang/rust/enum.md +++ b/dev/lang/rust/enum.md @@ -29,3 +29,21 @@ fn main() { } } ``` + +# Usages. + +## Match. + +```rust +enum Message { + Color(u8, u8, u8), + Quit +} + +fn process(message: Message) { + match message { + Message::Quit => quit(), + Message::Color(r, g, b) => process((r, g, b)) + } +} +``` diff --git a/dev/lang/rust/match.md b/dev/lang/rust/match.md new file mode 100644 index 0000000..00d31d2 --- /dev/null +++ b/dev/lang/rust/match.md @@ -0,0 +1,21 @@ +# Match (switch). + +# Example. + +Parentheses around match are optional, like `match number {`. + +```rust +let number = 10; + +match (number) { + 1 => foo(), + 2 | 3 | 4 => foo(), + 13..19 => foo(), + _ => default() +} + +let expr = match (number > 10) { + true => 1, + false => 0, +}; +``` diff --git a/dev/lang/rust/module.md b/dev/lang/rust/module.md new file mode 100644 index 0000000..e69de29 diff --git a/dev/lang/rust/string.md b/dev/lang/rust/string.md new file mode 100644 index 0000000..4da8e86 --- /dev/null +++ b/dev/lang/rust/string.md @@ -0,0 +1,77 @@ +# String. + +# Types. + +## String (owned). + +Those are familiar strings. + +```rust +let text: String = String::from("Text!"); +let text: String = "Text!".to_string(); +``` + +## &str (slice). + +Those are slices. + +```rust +let text: &str = "Text!"; + +let text: String = "Text!".to_string(); +let ref: &str = &text; +``` + +# Operations. + +## Append. + +```rust +let mut s = String::from("foo"); +s.push_str("bar"); +``` + +## Format. + +```rust +let s1 = String::from("tic"); +let s2 = String::from("tac"); +let s3 = String::from("toe"); + +let s = format!("{s1}-{s2}-{s3}"); +``` + +## Trim. + +```rust +fn trim_me(input: &str) -> String { + input.trim().to_string() +} +``` + +## Replace. + +```rust +fn replace_me(input: &str) -> String { + input.replace("cars", "balloons").to_string() +} +``` + +## Slice. + +```rust +let s = String::from("Здравствуйте"); +let s = &s[0..4]; // Content is "Зд" - first 4 bytes (2 bytes per letter). +``` + +## Iterate. + +```rust +// Iterate over UTF character vector. +for c in "Зд".chars() {} + +// Iterate over UTF bytes. +for c in "Зд".bytes() {} + +// To iterate over UTF letters you need to use an external crate. +```