rust/src/test/ui/traits/trait-coercion-generic.rs

26 lines
443 B
Rust
Raw Normal View History

// run-pass
#![allow(dead_code)]
2013-12-26 23:38:28 -06:00
trait Trait<T> {
fn f(&self, x: T);
}
2015-03-30 08:38:27 -05:00
#[derive(Copy, Clone)]
2013-12-26 23:38:28 -06:00
struct Struct {
x: isize,
y: isize,
2013-12-26 23:38:28 -06:00
}
impl Trait<&'static str> for Struct {
fn f(&self, x: &'static str) {
println!("Hi, {}!", x);
2013-12-26 23:38:28 -06:00
}
}
pub fn main() {
let a = Struct { x: 1, y: 2 };
2019-05-28 13:47:21 -05:00
let b: Box<dyn Trait<&'static str>> = Box::new(a);
2014-02-06 16:38:33 -06:00
b.f("Mary");
2019-05-28 13:47:21 -05:00
let c: &dyn Trait<&'static str> = &a;
2014-02-06 16:38:33 -06:00
c.f("Joe");
2013-12-26 23:38:28 -06:00
}