直接函数调用和 self::function 调用在 Rust 中总是一样的吗?
Is a direct function call and a self::function call always the same in rust?
最近开始学习rust。我目前在第 7.4 节(将路径纳入范围)。努力尝试,但我无法理解 self::some_sort_of_identifier
在 rust 中的用途。请您解释一下 use self::module_name::function_name
和 use module_name::function_name
之间的区别是什么?我都试过了,它们都在下面的例子中按预期工作:
mod my_mod {
pub fn func() {
print!("I'm here!");
}
}
use my_mod::func;
fn main() {
func();
}
运行 这个程序,正如预期的那样,我可以看到这个语句打印到终端中:
I'm here
这里的程序给了我完全相同的结果,rust 编译器没有任何抱怨:
mod my_mod {
pub fn func() {
print!("I'm here!");
}
}
use self::my_mod::func;
fn main() {
func();
}
那么,self::
生锈就没用了吗?当我可以像这样直接调用它时,为什么还要使用 self::my_mod::my_function();
:my_mod::my_function();
.
有没有他们可以推迟的情况?
对于您的用例,它主要是 2015 年 Rust 版本的遗留物。
在此版本中,以下代码无法编译:
use my_mod::func;
mod my_mod {
use my_mod2::func2;
pub fn func() {
func2();
}
mod my_mod2 {
pub fn func2() {
print!("I'm here!");
}
}
}
fn main() {
func();
}
编译器抱怨:
error[E0432]: unresolved import my_mod2
--> src\main.rs:4:9
|
| use my_mod2::func2;
| ^^^^^^^ help: a similar path exists: self::my_mod2
为什么改变了?可以看到关于路径和模块系统变化的注释here.
Rust 2018 simplifies and unifies path handling compared to Rust 2015. In Rust
2015, paths work differently in use declarations than they do
elsewhere. In particular, paths in use declarations would always start
from the crate root, while paths in other code implicitly started from
the current scope. Those differences didn't have any effect in the
top-level module, which meant that everything would seem
straightforward until working on a project large enough to have
submodules.
In Rust 2018, paths in use declarations and in other code work the
same way, both in the top-level module and in any submodule. You can
use a relative path from the current scope, a path starting from an
external crate name, or a path starting with crate, super, or self.
语言团队的博客postAnchored and Uniform Paths也强调了这一点
The uniformity is a really big advantage, and the specific feature we’re changing -
no longer having to use self::
- is something I know is a big
stumbling block for new users and a big annoyance for advanced users
(I’m always having to edit and recompile because I tried to import
from a submodule without using self).
然而,关键字本身在 use
语句中仍然有用,可以引用路径本身中的当前模块。喜欢
use std::io::{self, Read};
同
use std::io;
use std::io::Read;
最近开始学习rust。我目前在第 7.4 节(将路径纳入范围)。努力尝试,但我无法理解 self::some_sort_of_identifier
在 rust 中的用途。请您解释一下 use self::module_name::function_name
和 use module_name::function_name
之间的区别是什么?我都试过了,它们都在下面的例子中按预期工作:
mod my_mod {
pub fn func() {
print!("I'm here!");
}
}
use my_mod::func;
fn main() {
func();
}
运行 这个程序,正如预期的那样,我可以看到这个语句打印到终端中:
I'm here
这里的程序给了我完全相同的结果,rust 编译器没有任何抱怨:
mod my_mod {
pub fn func() {
print!("I'm here!");
}
}
use self::my_mod::func;
fn main() {
func();
}
那么,self::
生锈就没用了吗?当我可以像这样直接调用它时,为什么还要使用 self::my_mod::my_function();
:my_mod::my_function();
.
有没有他们可以推迟的情况?
对于您的用例,它主要是 2015 年 Rust 版本的遗留物。 在此版本中,以下代码无法编译:
use my_mod::func;
mod my_mod {
use my_mod2::func2;
pub fn func() {
func2();
}
mod my_mod2 {
pub fn func2() {
print!("I'm here!");
}
}
}
fn main() {
func();
}
编译器抱怨:
error[E0432]: unresolved import
my_mod2
--> src\main.rs:4:9
|
| use my_mod2::func2;
| ^^^^^^^ help: a similar path exists:self::my_mod2
为什么改变了?可以看到关于路径和模块系统变化的注释here.
Rust 2018 simplifies and unifies path handling compared to Rust 2015. In Rust 2015, paths work differently in use declarations than they do elsewhere. In particular, paths in use declarations would always start from the crate root, while paths in other code implicitly started from the current scope. Those differences didn't have any effect in the top-level module, which meant that everything would seem straightforward until working on a project large enough to have submodules.
In Rust 2018, paths in use declarations and in other code work the same way, both in the top-level module and in any submodule. You can use a relative path from the current scope, a path starting from an external crate name, or a path starting with crate, super, or self.
语言团队的博客postAnchored and Uniform Paths也强调了这一点
The uniformity is a really big advantage, and the specific feature we’re changing - no longer having to use
self::
- is something I know is a big stumbling block for new users and a big annoyance for advanced users (I’m always having to edit and recompile because I tried to import from a submodule without using self).
然而,关键字本身在 use
语句中仍然有用,可以引用路径本身中的当前模块。喜欢
use std::io::{self, Read};
同
use std::io;
use std::io::Read;