rust : wip.
This commit is contained in:
parent
84088e2e6d
commit
2aa8e03572
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
21
dev/lang/rust/match.md
Normal file
21
dev/lang/rust/match.md
Normal file
|
@ -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,
|
||||||
|
};
|
||||||
|
```
|
0
dev/lang/rust/module.md
Normal file
0
dev/lang/rust/module.md
Normal file
77
dev/lang/rust/string.md
Normal file
77
dev/lang/rust/string.md
Normal file
|
@ -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.
|
||||||
|
```
|
Loading…
Reference in a new issue