SHARE
TWEET

Untitled

a guest Oct 15th, 2019 103 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. trait A{
  2.     fn func(&self);
  3. }
  4. struct SA;
  5. impl A for SA{
  6.     fn func(&self) {
  7.         println!("hello");
  8.     }
  9. }
  10.  
  11. // pass by impl Trait
  12. trait B {
  13.     fn func(a: impl A);
  14. }
  15.  
  16. // pass by function type parameter
  17. trait C {
  18.     fn func<T: A>(a: T);
  19. }
  20.  
  21.  
  22. // pass by trait bound
  23. trait D<T> {
  24.     fn func(a: T);
  25. }
  26.  
  27. struct SB;
  28. impl B for SB {
  29.     fn func(a: impl A) {
  30.         a.func();
  31.     }
  32. }
  33.  
  34. struct SC;
  35. impl C for SC {
  36.     fn func<T: A>(a: T) {
  37.         a.func();
  38.     }
  39. }
  40.  
  41. struct SD;
  42. impl<T: A> D<T> for SD {
  43.     fn func(a: T) {
  44.         a.func();
  45.     }
  46. }
  47.  
  48.  
  49. // DOES NOT WORK
  50.  
  51. // struct SC2<T: A> {
  52. //     par: T,
  53. // }
  54. // // use bound from impl for type parameter of trait method
  55. // impl<T: A> C for SC2<T> {
  56. //     fn func<T>(a: T) {
  57. //         a.func();
  58. //     }
  59. // }
  60.  
  61. fn main() -> std::io::Result<()> {
  62.     SB::func(SA{});
  63.     SC::func(SA{});
  64.     SD::func(SA{});
  65.    
  66.     SC2::func(SA{});
  67.    
  68.     Ok(())
  69. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top