知识卡片
导入保留来源信息
Rust 常引入函数的父模块,以 module::function() 指示它来自何处,而结构体或枚举通常直接导入类型名。相同范围若要使用多个同名类型,可保留模块限定,例如 fmt::Result、io::Result,也可用 as 把其中一个改成清晰的局部别名。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- 用包、crate 和模块管理增长中的项目 Bringing Paths into Scope with the use Keyword · 查看原书依据
Although both Listing 7-11 and Listing 7-13 accomplish the same task, Listing 7-11 is the idiomatic way to bring a function into scope with use. Bringing the function’s parent module into scope with use means we have to specify the parent module when calling the function.
参考来源
- 位置:《The Rust Programming Language(第2版)》第07章 用包、crate 和模块管理增长中的项目(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c07.xhtml`,块 0148)
- 结论依据:原文使用两种函数导入的等价结果解释保留模块限定为何提高可读性。
- 原始内容:Although both Listing 7-11 and Listing 7-13 accomplish the same task, Listing 7-11 is the idiomatic way to bring a function into scope with use. Bringing the function’s parent module into scope with use means we have to specify the parent module when calling the function.