[英]can't use implemented trait in other file rust
所以我有两个文件 main.rs 和 utils.rs
我在 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
}
...
但我已经在 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 {
...
}
...
}
...
为什么我的实现不起作用,有什么办法可以解决这个问题,或者我只能在 main.rs 上实现 StringUtils?
在 Rust 编程语言中的模块树中引用项目的路径部分中出现了一个实质上等效的示例(如果您还没有阅读,我建议您阅读)。
简短的版本是您希望其他模块可见的模块中的任何项目(例如,特征,function 定义)应该具有pub
可见性修饰符的某种变体。 在您的即时示例中,这表明需要制作StringUtils
特征pub
(或将其暴露给包含模块的其他一些变体)。
事实上,如果您尝试通过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 {
| ^^^^^^^^^^^^^^^^^
这将链接到对一种修复方法的解释。 因此,如果我们改为执行pub trait StringUtils {... }
,则没有与使用该特征相关的问题。
您仍然会遇到@trentcl 提到的关于slice
的参数数量不正确的问题,我认为self.start..self.current
(或包含版本)应该是传递的范围。
最后,有一个与text
类型注释相关的错误,因为StringUtils::slice
将返回&str
,而不是String
。 根据您的需要,您应该更改特征及其实现,或者查看go between &str and String的方法以及它们之间的差异。
( 游乐场)。
您可能希望有一个更严格的可见性修饰符,例如pub(crate)
或pub(super)
分别限制对包含板条箱或包含模块的可见性。
可以在 The Rust Reference 的相关部分中找到对此的更详尽的解释。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.