mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 18:12:43 +00:00
31 lines
628 B
Rust
31 lines
628 B
Rust
// ./src/generics/where.md
|
|
|
|
|
|
use std::fmt::Debug;
|
|
|
|
trait PrintInOption {
|
|
fn print_in_option(self);
|
|
}
|
|
|
|
// Because we would otherwise have to express this as `T: Debug` or
|
|
// use another method of indirect approach, this requires a `where` clause:
|
|
impl<T> PrintInOption for T where
|
|
Option<T>: Debug {
|
|
// We want `Option<T>: Debug` as our bound because that is what's
|
|
// being printed. Doing otherwise would be using the wrong bound.
|
|
fn print_in_option(self) {
|
|
println!("{:?}", Some(self));
|
|
}
|
|
}
|
|
|
|
fn part0() {
|
|
let vec = vec![1, 2, 3];
|
|
|
|
vec.print_in_option();
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|