2016-09-09 12:51:14 +02:00
|
|
|
struct Struct(i32);
|
|
|
|
|
|
|
|
trait Trait {
|
|
|
|
fn method(&self);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Trait for Struct {
|
|
|
|
fn method(&self) {
|
|
|
|
assert_eq!(self.0, 42);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-21 18:00:04 +02:00
|
|
|
struct Foo<T: ?Sized>(T);
|
|
|
|
|
2016-09-09 12:51:14 +02:00
|
|
|
fn main() {
|
2019-05-30 10:58:30 +02:00
|
|
|
let y: &dyn Trait = &Struct(42);
|
2016-09-09 12:51:14 +02:00
|
|
|
y.method();
|
2016-09-21 18:00:04 +02:00
|
|
|
let x: Foo<Struct> = Foo(Struct(42));
|
2019-05-30 10:58:30 +02:00
|
|
|
let y: &Foo<dyn Trait> = &x;
|
2016-09-21 18:00:04 +02:00
|
|
|
y.0.method();
|
2017-07-19 11:38:15 -07:00
|
|
|
|
2019-05-30 10:58:30 +02:00
|
|
|
let x: Box<dyn Fn(i32) -> i32> = Box::new(|x| x * 2);
|
2016-09-09 15:44:35 +02:00
|
|
|
assert_eq!(x(21), 42);
|
|
|
|
let mut i = 5;
|
|
|
|
{
|
2019-05-30 10:58:30 +02:00
|
|
|
let mut x: Box<dyn FnMut()> = Box::new(|| i *= 2);
|
2016-09-09 15:44:35 +02:00
|
|
|
x(); x();
|
|
|
|
}
|
|
|
|
assert_eq!(i, 20);
|
2016-09-09 12:51:14 +02:00
|
|
|
}
|