知识卡片
导入名称受作用域限制
use 给能够访问的既有路径建立当前作用域内的简称;它也受隐私检查。父作用域引入的 hosting 不会自动成为子模块中的裸名。调用移入子模块后,可把 use 一起移入,或通过 super::hosting 显式引用父级的导入名。
相关卡片 [[模块可见性逐段检查]]
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 用包、crate 和模块管理增长中的项目 Bringing Paths into Scope with the use Keyword · 查看原书依据
Note that use only creates the shortcut for the particular scope in which the use occurs. Listing 7-12 moves the eat_at_restaurant function into a new child module named customer, which is then a different scope than the use statement, so the function body won’t compile.
参考来源
- 位置:《The Rust Programming Language(第2版)》第07章 用包、crate 和模块管理增长中的项目(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c07.xhtml`,块 0136)
- 结论依据:将调用移进 customer 模块后的失败,直接证明 use 的简称只在相应作用域可用。
- 原始内容:Note that use only creates the shortcut for the particular scope in which the use occurs. Listing 7-12 moves the eat_at_restaurant function into a new child module named customer, which is then a different scope than the use statement, so the function body won’t compile.