Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait A{
- fn func(&self);
- }
- struct SA;
- impl A for SA{
- fn func(&self) {
- println!("hello");
- }
- }
- // pass by impl Trait
- trait B {
- fn func(a: impl A);
- }
- // pass by function type parameter
- trait C {
- fn func<T: A>(a: T);
- }
- // pass by trait bound
- trait D<T> {
- fn func(a: T);
- }
- struct SB;
- impl B for SB {
- fn func(a: impl A) {
- a.func();
- }
- }
- struct SC;
- impl C for SC {
- fn func<T: A>(a: T) {
- a.func();
- }
- }
- struct SD;
- impl<T: A> D<T> for SD {
- fn func(a: T) {
- a.func();
- }
- }
- // DOES NOT WORK
- // struct SC2<T: A> {
- // par: T,
- // }
- // // use bound from impl for type parameter of trait method
- // impl<T: A> C for SC2<T> {
- // fn func<T>(a: T) {
- // a.func();
- // }
- // }
- fn main() -> std::io::Result<()> {
- SB::func(SA{});
- SC::func(SA{});
- SD::func(SA{});
- SC2::func(SA{});
- Ok(())
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement