mirror of
https://github.com/LukasKalbertodt/programmieren-in-rust.git
synced 2024-11-17 18:48:56 +01:00
Add additional material presented in the monday lecture
This commit is contained in:
parent
92f68996ab
commit
a42665efbe
66
materialien/animal.rs
Executable file
66
materialien/animal.rs
Executable file
@ -0,0 +1,66 @@
|
||||
trait Animal {
|
||||
fn speak(&self);
|
||||
}
|
||||
|
||||
struct Dog {
|
||||
name: String,
|
||||
}
|
||||
|
||||
impl Animal for Dog {
|
||||
fn speak(&self) {
|
||||
println!("Wuff {}", self.name);
|
||||
}
|
||||
}
|
||||
|
||||
struct Cat;
|
||||
|
||||
impl Animal for Cat {
|
||||
fn speak(&self) {
|
||||
println!("meow");
|
||||
}
|
||||
}
|
||||
|
||||
/// There are basically three different ways you can write a
|
||||
/// function.
|
||||
///
|
||||
/// ```
|
||||
/// fn foo<T: Bar>(x: T) // version A
|
||||
/// fn foo(x: &Bar) // version B
|
||||
/// fn foo<T: Bar + ?Sized>(x: T) // version C
|
||||
/// ```
|
||||
///
|
||||
/// - A: static dispatch only, monomorphization
|
||||
/// - B: dynamic dispatch only, accepting a trait object
|
||||
/// - C: static dispatch, but passing a trait object is allowed;
|
||||
/// this is useful if you want to allow both
|
||||
///
|
||||
fn speak_twice<T: Animal + ?Sized>(a: &T) {
|
||||
a.speak();
|
||||
a.speak();
|
||||
}
|
||||
|
||||
/// We can't return `Animal` directly, because it is unsised.
|
||||
/// So we need to hide it behind a pointer. But `&Animal` doesn't
|
||||
/// work, because we would reference variables from the function.
|
||||
/// To return an owned trait object, we use `Box<Animal>`.
|
||||
fn get_user_animal() -> Box<Animal> {
|
||||
println!("If you want a dog, give me a name:");
|
||||
let mut dog_name = String::new();
|
||||
std::io::stdin().read_line(&mut dog_name).expect("oh noe!");
|
||||
|
||||
let new_len = dog_name.trim_right().len();
|
||||
dog_name.truncate(new_len);
|
||||
|
||||
|
||||
if dog_name.is_empty() {
|
||||
Box::new(Cat)
|
||||
} else {
|
||||
Box::new(Dog { name: dog_name })
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let a = get_user_animal();
|
||||
|
||||
speak_twice(&*a);
|
||||
}
|
29
materialien/tipps-abschlussprojekt.md
Executable file
29
materialien/tipps-abschlussprojekt.md
Executable file
@ -0,0 +1,29 @@
|
||||
Tipps fürs Abschlussprojekt:
|
||||
============================
|
||||
|
||||
### Crates finden
|
||||
|
||||
Passende Crates finden ist mit `crates.io` noch ein bisschen schwierig. Daher:
|
||||
|
||||
- [**`stdx`**](https://github.com/brson/stdx): Die wohl wichtigsten Rust-Crates. Hier sollte man zuerst schauen, wenn man etwas sucht. Außerdem ist es auch sinnvoll, sich die Crates einfach mal so anzuschauen, um zu sehen, was es schon alles gibt.
|
||||
- [**Awesome Rust**](https://github.com/kud1ing/awesome-rust): Lange Liste mit Crates zu jedem Thema.
|
||||
|
||||
|
||||
### Travis
|
||||
|
||||
Continuous Integration Services sind super hilfreich, gerade bei stark typisierten Sprachen und TDD Projekten. Travis ist ein solcher Service, den ihr kostenlos nutzen und extrem schnell einrichten könnt. Mehr Informationen gibt es z.B. [hier](https://docs.travis-ci.com/user/languages/rust/). Aber in kurz:
|
||||
|
||||
1. Eine `.travis.yml` in eurem Repository mit folgendem Inhalt anlegen:
|
||||
|
||||
```
|
||||
language: rust
|
||||
rust:
|
||||
- stable
|
||||
- beta
|
||||
- nightly
|
||||
matrix:
|
||||
allow_failures:
|
||||
- rust: nightly
|
||||
```
|
||||
|
||||
2. Auf `travis-ci.com` mit GitHub Account anmelden und Repository aktivieren.
|
Loading…
Reference in New Issue
Block a user