简体   繁体   English

不能在其他文件中使用已实现的特征 rust

[英]can't use implemented trait in other file rust

so i have got two files main.rs and utils.rs所以我有两个文件 main.rs 和 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我在 utils.rs 上实现了 StringUtils 方法,但是当我尝试在 main.rs 中使用该方法时,它给了我这个错误

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 上实现了它

// 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?为什么我的实现不起作用,有什么办法可以解决这个问题,或者我只能在 main.rs 上实现 StringUtils?

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).在 Rust 编程语言中的模块树中引用项目的路径部分中出现了一个实质上等效的示例(如果您还没有阅读,我建议您阅读)。

The short version is that any item (eg, 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.简短的版本是您希望其他模块可见的模块中的任何项目(例如,特征,function 定义)应该具有pub可见性修饰符的某种变体。 In your instant example, this manifests as needing to make the StringUtils trait pub (or some other variant exposing it to the containing module).在您的即时示例中,这表明需要制作StringUtils特征pub (或将其暴露给包含模块的其他一些变体)。

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:事实上,如果您尝试通过use utils::StringUtils而不是 glob 导入直接导入StringUtils ,您会收到以下错误消息:

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.因此,如果我们改为执行pub trait StringUtils {... } ,则没有与使用该特征相关的问题。

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.您仍然会遇到@trentcl 提到的关于slice的参数数量不正确的问题,我认为self.start..self.current (或包含版本)应该是传递的范围。

Finally, there is an error relating to your type annotation of text as StringUtils::slice would return &str , not String .最后,有一个与text类型注释相关的错误,因为StringUtils::slice将返回&str ,而不是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 .根据您的需要,您应该更改特征及其实现,或者查看go between &str and String的方法以及它们之间的差异

( 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.您可能希望有一个更严格的可见性修饰符,例如pub(crate)pub(super)分别限制对包含板条箱或包含模块的可见性。

A more exhaustive explanation of this can be found inthe relevant section in The Rust Reference .可以在 The Rust Reference 的相关部分中找到对此的更详尽的解释。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM