mirror of
https://github.com/ahgamut/rust-ape-example.git
synced 2024-05-15 18:12:43 +00:00
36 lines
811 B
Rust
36 lines
811 B
Rust
// ./src/generics/bounds/testcase_empty.md
|
|
|
|
|
|
struct Cardinal;
|
|
struct BlueJay;
|
|
struct Turkey;
|
|
|
|
trait Red {}
|
|
trait Blue {}
|
|
|
|
impl Red for Cardinal {}
|
|
impl Blue for BlueJay {}
|
|
|
|
// These functions are only valid for types which implement these
|
|
// traits. The fact that the traits are empty is irrelevant.
|
|
fn red<T: Red>(_: &T) -> &'static str { "red" }
|
|
fn blue<T: Blue>(_: &T) -> &'static str { "blue" }
|
|
|
|
fn part0() {
|
|
let cardinal = Cardinal;
|
|
let blue_jay = BlueJay;
|
|
let _turkey = Turkey;
|
|
|
|
// `red()` won't work on a blue jay nor vice versa
|
|
// because of the bounds.
|
|
println!("A cardinal is {}", red(&cardinal));
|
|
println!("A blue jay is {}", blue(&blue_jay));
|
|
//println!("A turkey is {}", red(&_turkey));
|
|
// ^ TODO: Try uncommenting this line.
|
|
}
|
|
|
|
pub fn main() {
|
|
part0();
|
|
}
|
|
|