fork(3) download
  1. trait FooPow {
  2. fn pow(&self) -> i32;
  3. }
  4.  
  5. struct Foo(i32);
  6.  
  7. impl<'a> FooPow for &'a Foo {
  8. fn pow(&self) -> i32 {
  9. self.0 * self.0
  10. }
  11. }
  12.  
  13. /*impl<'a> FooPow for &'a mut Foo {
  14.   fn pow(&self) -> i32 {
  15.   self.0 * self.0
  16.   }
  17. }*/
  18.  
  19. fn test1(x: &Foo) {
  20. println!("{}", x.pow());
  21. }
  22.  
  23. fn test2(x: &mut Foo) {
  24. println!("{}", x.pow()); // no method named `pow` found for type `&mut Foo` in the current scope
  25. }
  26.  
  27. fn main() {
  28. test1(&Foo(42));
  29. test2(&mut Foo(42));
  30. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
error: no method named `pow` found for type `&mut Foo` in the current scope
  --> prog.rs:24:22
   |
24 |     println!("{}", x.pow()); // no method named `pow` found for type `&mut Foo` in the current scope
   |                      ^^^
   |
   = help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `pow`, perhaps you need to implement it:
   = help: candidate #1: `FooPow`

error: aborting due to previous error

stdout
Standard output is empty