Reputation: 27
so i have got two files main.rs and utils.rs
I implemented StringUtils method on utils.rs but when I try to use the method in main.rs it gives me this error
error[E0599]: no method named `slice` found for reference `&str` in the current scope --> src\main.rs:89:50 | 89 | let text: String = self.inner.clone().as_str().slice(self.start, self.current); | ^^^^^ method not found in `&str` | = help: items from traits can only be used if the trait is implemented and in scope note: `StringUtils` defines an item `slice`, perhaps you need to implement it --> src\util.rs:25:1 | 25 | trait StringUtils { | ^^^^^^^^^^^^^^^^^
// main.rs
mod utils;
use utils::*;
...
fn add_token0(&mut self, token_type: TokenType) {
let text: String = self.inner.clone().as_str().slice(self.start, self.current);
// error: no method named `slice` found for reference `&str` in the current scope
}
...
but I implemented it already on utils.rs
// utils.rs
...
trait StringUtils {
...
fn slice(&self, range: impl RangeBounds<usize>) -> &str;
...
}
impl StringUtils for str {
...
fn slice(&self, range: impl RangeBounds<usize>) -> &str {
...
}
...
}
...
why doesn't my implementation work, and is there any way to solve it or I can only implement StringUtils on main.rs?
Upvotes: 0
Views: 1582
Reputation: 1732
A substantively equivalent example appears in the section Paths for Referring to an Item in the Module Tree in The Rust Programming Language (which if you haven't read, I would suggest).
The short version is that any item (e.g., trait, function definition) within a module that you would like to be visible to other modules should have some variant of a pub
visibility modifier. In your instant example, this manifests as needing to make the StringUtils
trait pub
(or some other variant exposing it to the containing module).
In fact, if you attempt to import StringUtils
directly, via use utils::StringUtils
instead of a glob import, you'd get the following error message:
error[E0603]: trait `StringUtils` is private
--> src/lib.rs:7:12
|
7 | use utils::StringUtils;
| ^^^^^^^^^^^ private trait
|
note: the trait `StringUtils` is defined here
--> src/lib.rs:19:5
|
19 | trait StringUtils {
| ^^^^^^^^^^^^^^^^^
Which would link to this explanation of one way to fix it. So if we do pub trait StringUtils { ... }
instead, there are no issues relating to using the trait.
You would still have the issue @trentcl mentions concerning the incorrect number of parameters to slice
, and I presume self.start..self.current
(or the inclusive version) should be the range passed instead.
Finally, there is an error relating to your type annotation of text
as StringUtils::slice
would return &str
, not String
. Depending on what you want, you should either change the trait and its implementations or take a look at ways to go between &str and String and the differences between them.
(playground).
You may want to have a more restrictive visibility modifier, like pub(crate)
or pub(super)
which restrict visibility to the containing crate or the containing module, respectively.
A more exhaustive explanation of this can be found in the relevant section in The Rust Reference.
Upvotes: 3