Trait implementations, trait method not found?

I definitely don't understand what's going on here; I need help understanding 2 things:

  1. What the error is telling me.
  2. How I should be able to do what I am trying to do

I am making a query and I'd like to see what the sql looks like before sending it and it looks like because there's a trait implementation for the type, that I should be able to just run the function associated, but apparently no?

Something like this seems like it should be possible, but I definitely don't understand a lot here. Docs: Query in sqlx::query - Rust

let q = sqlx::query("SELECT * FROM test WHERE item1 = $1)
.bind(test_item);
println!("{}",q.sql());

I am getting the error: items from traits can only be used if the trait is in scope

Hereā€™s an example of such an error

mod m {
    pub trait Foo {
        fn foo(&self) {}
    }
    impl Foo for () {
        fn foo(&self) {}
    }
}

fn f() {
    ().foo();
}
   Compiling playground v0.0.1 (/playground)
error[E0599]: no method named `foo` found for unit type `()` in the current scope
  --> src/lib.rs:11:8
   |
3  |         fn foo(&self) {}
   |            --- the method is available for `()` here
...
11 |     ().foo();
   |        ^^^ method not found in `()`
   |
   = help: items from traits can only be used if the trait is in scope
help: the following trait is implemented but not in scope; perhaps add a `use` for it:
   |
1  | use crate::m::Foo;
   |

For more information about this error, try `rustc --explain E0599`.
error: could not compile `playground` due to previous error

and hereā€™s how to fix it

  mod m {
      pub trait Foo {
          fn foo(&self) {}
      }
      impl Foo for () {
          fn foo(&self) {}
      }
  }
  
+ use m::Foo;
+ 
  fn f() {
      ().foo();
  }

Also, as you can see, a full error message is significantly longer than what you posted, and the ā€œitems from traits can only be used if the trait is in scopeā€ part is not even the error message at all, but merely a section of the additional ā€œhelpā€ remarks the compiler gives. In case you need further help with the type and trait at hand, feel free to pose a complete error message as e.g. obtained by running cargo check from the terminal in the project directory :slight_smile:

1 Like

You can only call trait methods, e.g. .some_method(), when the corresponding trait is in scope, e.g. imported via use some_crate::SomeTrait (assuming that some_method is a method of SomeTrait).

If you paste the full error message, we might be able to help you in regard to which trait you missed to import.

Maybe it helps to import the sqlx::prelude with:

use sqlx::prelude::*`;

Note that you can also import traits in an unnameable way if you want to keep your namespace clean, e.g.:

use sqlx::Statement as _;

or

use sqlx::Execute as _;

I'm not sure which trait you are missing, but those two are ones which provide an .sql method.

2 Likes

That was exactly the info needed. Thanks!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.