session results
This commit is contained in:
parent
ec9c187638
commit
84c28a25e9
3 changed files with 169 additions and 2 deletions
|
@ -22,7 +22,11 @@
|
|||
* Using Structs to Structure Related Data
|
||||
* Enums and Pattern Matching
|
||||
|
||||
### 09.05.
|
||||
### 09.05. 15:00
|
||||
|
||||
* Managing Growing Projects with Packages, Crates, and Modules
|
||||
* Common Collections
|
||||
|
||||
### 30.05.
|
||||
### 27.06.
|
||||
### 25.07.
|
|
@ -15,4 +15,4 @@ struct User {
|
|||
* Groß == Public / Klein == private?
|
||||
* String::from ist schon viel schreibarbeit ...
|
||||
* Lebensdauer wird interessant ...
|
||||
* automatische Refernzierung / Dereferenzierung - nett :-)
|
||||
* automatische Referenzierung / Dereferenzierung - nett :-)
|
||||
|
|
|
@ -2,3 +2,166 @@
|
|||
|
||||
## Understanding Ownership
|
||||
|
||||
### take
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s1 = String::from("Hello");
|
||||
let s2 = s1.clone();
|
||||
|
||||
s1.push_str(" World!");
|
||||
|
||||
takes_ownership(s1);
|
||||
}
|
||||
|
||||
fn takes_ownership(some_string: String) {
|
||||
println!("taken: {some_string}");
|
||||
}
|
||||
```
|
||||
|
||||
* Ownership um GC zu vermeiden!
|
||||
* pass gut zu DDD Aggregaten
|
||||
* Das Problem der Heap-Speicher-Fragmentierung bleibt. Wie löst Rust das?
|
||||
|
||||
### borrow
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s1 = String::from("Hello");
|
||||
|
||||
borrow_ownership(&mut s1);
|
||||
|
||||
println!("s1: {s1}");
|
||||
}
|
||||
|
||||
fn borrow_ownership(some_string: &mut String) {
|
||||
some_string.push_str(" do illegal things!");
|
||||
println!("borrowed: {some_string}");
|
||||
}
|
||||
```
|
||||
|
||||
* Borrow == by reference
|
||||
* Was passiert, wenn der Eigentümer nebenläufig den Wert ändert?
|
||||
* Scope == Stack?
|
||||
* Gibts in Rust weniger StackOverflows bei Rekursion?
|
||||
|
||||
### slice
|
||||
|
||||
```rust
|
||||
fn main() {
|
||||
let mut s4 = String::from("hello world");
|
||||
let s4a = &s4[0..5];
|
||||
//s4 = s4.replace(" world", "xworld");
|
||||
|
||||
println!("s4: {s4}\ns4a: -{s4a}-");
|
||||
}
|
||||
```
|
||||
|
||||
* &String und &str ist jetzt nicht intuitiv ...
|
||||
* Kann man borrow zurückgeben?
|
||||
|
||||
* nicht erlaubt:
|
||||
```
|
||||
let mut s = String::from("hello");
|
||||
|
||||
let r1 = &s;
|
||||
let r2 = &s;
|
||||
let r3 = &mut s; // Das ist nicht erlaubt
|
||||
|
||||
println!("{}, {}, and {}", r1, r2, r3);
|
||||
|
||||
```
|
||||
* erlaubt:
|
||||
```
|
||||
let mut s = String::from("hello");
|
||||
|
||||
let r1 = &s;
|
||||
let r2 = &s;
|
||||
println!("{r1} and {r2}");
|
||||
|
||||
let r3 = &mut s;
|
||||
println!("{r3}");
|
||||
```
|
||||
|
||||
## Structs
|
||||
|
||||
### Vererbung
|
||||
|
||||
```rust
|
||||
#[derive(Debug)]
|
||||
struct User {
|
||||
id: u64,
|
||||
name: String,
|
||||
}
|
||||
```
|
||||
|
||||
```
|
||||
let user2 = User {
|
||||
email: String::from("another@example.com"),
|
||||
..user1
|
||||
};
|
||||
```
|
||||
|
||||
Wie werden Defaults in structs definiert?
|
||||
|
||||
You can implement traits for types you didn't define [yourself], whereas interfaces can only be implemented for your own classes.
|
||||
|
||||
```
|
||||
struct Color(i32, i32, i32);
|
||||
struct Point(i32, i32, i32);
|
||||
|
||||
fn main() {
|
||||
let black = Color(0, 0, 0);
|
||||
let origin = Point(0, 0, 0);
|
||||
|
||||
let Point(x, y, z) = point
|
||||
}
|
||||
```
|
||||
|
||||
### Constructor
|
||||
|
||||
```
|
||||
impl Rectangle {
|
||||
fn square(size: u32) -> Self {
|
||||
Self {
|
||||
width: size,
|
||||
height: size,
|
||||
}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Enums
|
||||
|
||||
```rust
|
||||
enum IpAddr {
|
||||
V4(u8, u8, u8, u8),
|
||||
V6(String),
|
||||
}
|
||||
```
|
||||
|
||||
if let:
|
||||
```rust
|
||||
let config_max = Some(3u8);
|
||||
if let Some(max) = config_max {
|
||||
println!("The maximum is configured to be {max}");
|
||||
} else {
|
||||
println!("There is no maximum");
|
||||
}
|
||||
```
|
||||
|
||||
let else:
|
||||
|
||||
```rust
|
||||
fn describe_state_quarter(coin: Coin) -> Option<String> {
|
||||
let Coin::Quarter(state) = coin else {
|
||||
return None;
|
||||
};
|
||||
|
||||
if state.existed_in(1900) { // <--- WTF das kommt aus einer Klammer? Was falsch bei euch?
|
||||
Some(format!("{state:?} is pretty old, for America!"))
|
||||
} else {
|
||||
Some(format!("{state:?} is relatively new."))
|
||||
}
|
||||
}
|
||||
```
|
||||
|
|
Loading…
Add table
Reference in a new issue