Rust入门教程(三).html 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <!DOCTYPE html>
  2. <html lang="en-US">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1">
  6. <title>Rust 入门教程(三) | Rust训练营教程文档</title>
  7. <meta name="generator" content="VuePress 1.9.10">
  8. <link rel="icon" href="/rust_camp_tutorial/logo.png">
  9. <meta name="description" content="DragonOS-Rust camp">
  10. <link rel="preload" href="/rust_camp_tutorial/assets/css/0.styles.7dd9be3e.css" as="style"><link rel="preload" href="/rust_camp_tutorial/assets/js/app.d7ab8f65.js" as="script"><link rel="preload" href="/rust_camp_tutorial/assets/js/2.3dc1b8de.js" as="script"><link rel="preload" href="/rust_camp_tutorial/assets/js/1.7f771cfb.js" as="script"><link rel="preload" href="/rust_camp_tutorial/assets/js/26.606cfbc8.js" as="script"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/10.23a1f579.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/11.c389195a.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/12.1d996921.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/13.4d4410c4.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/14.37ef2a72.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/15.5542c093.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/16.d48fd1ce.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/17.bd8d538c.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/18.6d3b94c1.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/19.eb35cfee.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/20.c11ec329.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/21.db1b5d88.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/22.7714be7b.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/23.7f3a9620.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/24.2f88b37d.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/25.df99dd5f.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/27.928f1e6b.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/28.f61a69ee.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/29.802642cf.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/3.5322f14a.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/30.72f41aed.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/31.e1aa8cbc.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/32.30c1ff3b.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/33.f21667a4.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/4.84e1e480.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/5.f0541060.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/6.dfb06aa0.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/7.7551a9fb.js"><link rel="prefetch" href="/rust_camp_tutorial/assets/js/vendors~docsearch.5e19b665.js">
  11. <link rel="stylesheet" href="/rust_camp_tutorial/assets/css/0.styles.7dd9be3e.css">
  12. </head>
  13. <body>
  14. <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/rust_camp_tutorial/" class="home-link router-link-active"><img src="logo.png" alt="Rust训练营教程文档" class="logo"> <span class="site-name can-hide">Rust训练营教程文档</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/rust_camp_tutorial/" class="nav-link">
  15. 首页
  16. </a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/rust_camp_tutorial/" class="nav-link">
  17. 首页
  18. </a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group depth-0"><p class="sidebar-heading open"><span>Rust 入门教程(三)</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%89%EF%BC%89.html#_1-package-和-crate" class="sidebar-link">1. Package 和 Crate</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%89%EF%BC%89.html#_2-路径-path" class="sidebar-link">2. 路径(Path)</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%89%EF%BC%89.html#_3-use-关键字" class="sidebar-link">3. use 关键字</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/rust_camp_tutorial/Rust%E6%96%87%E6%A1%A3/Rust%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B%EF%BC%88%E4%B8%89%EF%BC%89.html#_4-将模块拆分为不同文件" class="sidebar-link">4. 将模块拆分为不同文件</a><ul class="sidebar-sub-headers"></ul></li></ul></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="rust-入门教程-三"><a href="#rust-入门教程-三" class="header-anchor">#</a> Rust 入门教程(三)</h1> <p><strong>Rust 的代码组织</strong></p> <p>代码组织主要包括:</p> <ul><li>哪些细节可以暴露,哪些细节是私有的一作用域内哪些名称有效</li> <li>模块系统:
  19. <ul><li>Package(包):Cargo 的特性,让你构建、测试、共享 crate</li> <li>Crate(单元包): 一个模块树,它可产生一个 library 或可执行文件</li> <li>Module(模块)、use:让你控制代码的组织、作用域、私有路径</li> <li>Path(路径):为 struct、function 或 module 等项命名的方式</li></ul></li></ul> <h2 id="_1-package-和-crate"><a href="#_1-package-和-crate" class="header-anchor">#</a> 1. Package 和 Crate</h2> <p>Crate的类型:</p> <ul><li>binary</li> <li>library</li></ul> <p>Crate Root:</p> <ul><li>是源代码文件</li> <li>Rust编译器从这里开始,组成你的 Crate 的根Module</li></ul> <p>一个 Package:</p> <ul><li>包含 1 个 Cargo.toml,它描述了如何构建这些 Crates</li> <li>只能包含 0-1 个 library crate</li> <li>可以包含任意数量的 binary crate</li> <li>但必须至少包含一个 crate (library 或 binary)</li></ul> <p><strong>Cargo 的惯例</strong></p> <p><code>src/main.rs</code>:</p> <ul><li>binary crate 的 crate root</li> <li>crate 名与 package 名相同</li></ul> <p><code>src/lib.rs</code>:</p> <ul><li>package 包含一个 library crate</li> <li>library crate 的 crate root</li> <li>crate 名与 package 名相同</li></ul> <p>Cargo 把 crate root 文件交给 rustc 来构建 library 或 binary</p> <p>如果 package 下有一个lib.rs,就说明 package 下面有一个 library crate,这个 lib.rs 就是这个 library crate 的入口文件,crate 名字也是叫 my-project.</p> <p>一个 Package 可以同时包含 src/main.r s和 src/lib.rs</p> <ul><li>一个 binary crate,一个 library crate</li> <li>名称与 package 名相同</li></ul> <p>一个 Package 可以有多个 binary crate:</p> <ul><li>文件放在 src/bin</li> <li>每个文件是单独的 binary crate</li></ul> <p><strong>Crate 的作用</strong></p> <p>将相关功能组合到一个作用域内,便于在项目间进行共享,防止冲突。例如 rand crate,访问它的功能需要通过它的名字:rand</p> <p><strong>定义 module 来控制作用域和私有性</strong></p> <p>Module:</p> <ul><li>在一个crate内,将代码进行分组</li> <li>增加可读性,易于复用</li> <li>控制项目(item)的私有性。public、private</li></ul> <p>建立 module:</p> <ul><li>mod 关键字</li> <li>可嵌套</li> <li>可包含其它项(struct、enum、常量、trait、函数等)的定义</li></ul> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">mod</span> <span class="token module-declaration namespace">front_of_house</span> <span class="token punctuation">{</span>
  20. <span class="token keyword">mod</span> <span class="token module-declaration namespace">hosting</span> <span class="token punctuation">{</span>
  21. <span class="token keyword">fn</span> <span class="token function-definition function">add_to_waitlist</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  22. <span class="token keyword">fn</span> <span class="token function-definition function">seat_at_table</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  23. <span class="token punctuation">}</span>
  24. <span class="token keyword">mod</span> <span class="token module-declaration namespace">serving</span> <span class="token punctuation">{</span>
  25. <span class="token keyword">fn</span> <span class="token function-definition function">take_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  26. <span class="token keyword">fn</span> <span class="token function-definition function">serve_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  27. <span class="token keyword">fn</span> <span class="token function-definition function">take_payment</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  28. <span class="token punctuation">}</span>
  29. <span class="token punctuation">}</span>
  30. </code></pre></div><p><img src="https://raw.githubusercontent.com/CherryYang05/PicGo-image/master/images/20220514204827.png" alt="crate树形结构"></p> <p><code>src/main.rs</code> 和 <code>src/lib.rs</code> 叫做 <code>crate roots</code>:</p> <ul><li>这两个文件(任意一个)的内容形成了名为 crate 的模块,位于整个模块树的根部</li> <li>整个模块树在隐式的crate模块下</li></ul> <h2 id="_2-路径-path"><a href="#_2-路径-path" class="header-anchor">#</a> 2. 路径(Path)</h2> <p>为了在 Rust 的模块中找到某个条目,需要使用路径。路径的两种形式:</p> <ul><li>绝对路径:从 crate root开始,使用 crate 名或字面值 crate</li> <li>相对路径:从当前模块开始,使用 self,super 或当前模块的标识符路径至少由一个标识符组成,标识符之间使用<code>::</code></li></ul> <p><strong>私有边界(privacy boundary)</strong></p> <ul><li>模块不仅可以组织代码,还可以定义私有边界</li> <li>如果想把函数或结构体设为私有,可以将它放到某个模块中</li> <li>Rust中所有的条目(函数,方法,struct,enum,模块,常量)默认是私有的</li> <li>父级模块无法访问子模块中的私有条目</li> <li>子模块里可以使用所有祖先模块中的条目</li></ul> <p><strong>Pub 关键字</strong></p> <p>使用 <code>pub</code> 关键字将某些条目标记为公共的,同为根下的模块尽管都是私有,也可以访问</p> <p><strong>super 关键字</strong></p> <p>用来访问父级模块路径中的内容,类似文件系统中的 <code>..</code>,例子如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">mod</span> <span class="token module-declaration namespace">back_of_house</span> <span class="token punctuation">{</span>
  31. <span class="token keyword">fn</span> <span class="token function-definition function">fix_incorrect_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  32. <span class="token function">cook_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  33. <span class="token keyword">super</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>serve_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  34. <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>serve_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  35. <span class="token punctuation">}</span>
  36. <span class="token keyword">fn</span> <span class="token function-definition function">cook_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  37. <span class="token punctuation">}</span>
  38. <span class="token keyword">fn</span> <span class="token function-definition function">serve_order</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  39. </code></pre></div><p><strong>pub struct</strong></p> <p>pub 放在 struct 前:</p> <ul><li>struct 是公共的</li> <li>struct 的字段默认是私有的</li> <li>struct 的字段需要<strong>单独设置</strong> pub 来变成公有</li></ul> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">mod</span> <span class="token module-declaration namespace">back_of_house</span> <span class="token punctuation">{</span>
  40. <span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">Breakfast</span> <span class="token punctuation">{</span>
  41. <span class="token keyword">pub</span> toast<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">,</span>
  42. fruit<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">,</span>
  43. <span class="token punctuation">}</span>
  44. <span class="token keyword">impl</span> <span class="token class-name">Breakfast</span> <span class="token punctuation">{</span>
  45. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">summer</span><span class="token punctuation">(</span>toast<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token keyword">str</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Breakfast</span> <span class="token punctuation">{</span>
  46. <span class="token class-name">Breakfast</span> <span class="token punctuation">{</span>
  47. toast<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">::</span><span class="token function">from</span><span class="token punctuation">(</span>toast<span class="token punctuation">)</span><span class="token punctuation">,</span>
  48. fruit<span class="token punctuation">:</span> <span class="token class-name">String</span><span class="token punctuation">::</span><span class="token function">from</span><span class="token punctuation">(</span><span class="token string">&quot;strawberry&quot;</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  49. <span class="token punctuation">}</span>
  50. <span class="token punctuation">}</span>
  51. <span class="token punctuation">}</span>
  52. <span class="token punctuation">}</span>
  53. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">eat_at_restaurant</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  54. <span class="token keyword">let</span> <span class="token keyword">mut</span> meal <span class="token operator">=</span> <span class="token namespace">back_of_house<span class="token punctuation">::</span></span><span class="token class-name">Breakfast</span><span class="token punctuation">::</span><span class="token function">summer</span><span class="token punctuation">(</span><span class="token string">&quot;Rye&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  55. <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;I'd like {} toast please.&quot;</span><span class="token punctuation">,</span> meal<span class="token punctuation">.</span>toast<span class="token punctuation">)</span><span class="token punctuation">;</span>
  56. <span class="token punctuation">}</span>
  57. </code></pre></div><p><strong>pub enum</strong></p> <p>pub 放在 enum 前:</p> <ul><li>enum 是公共的</li> <li><strong>enum 的变体也都是公共的</strong></li></ul> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">mod</span> <span class="token module-declaration namespace">back_of_house</span> <span class="token punctuation">{</span>
  58. <span class="token keyword">pub</span> <span class="token keyword">enum</span> <span class="token type-definition class-name">Appetizer</span> <span class="token punctuation">{</span>
  59. <span class="token class-name">Soup</span><span class="token punctuation">,</span>
  60. <span class="token class-name">Salad</span>
  61. <span class="token punctuation">}</span>
  62. <span class="token punctuation">}</span>
  63. </code></pre></div><h2 id="_3-use-关键字"><a href="#_3-use-关键字" class="header-anchor">#</a> 3. use 关键字</h2> <p>可以使用 <code>use</code> 将路径导入到作用域内,同样遵循私有性原则,即只有公共的才能被使用</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">mod</span> <span class="token module-declaration namespace">front_of_house</span> <span class="token punctuation">{</span>
  64. <span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">hosting</span> <span class="token punctuation">{</span>
  65. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">add_to_waitlist</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  66. <span class="token keyword">fn</span> <span class="token function-definition function">some_function</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  67. <span class="token punctuation">}</span>
  68. <span class="token punctuation">}</span>
  69. <span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>front_of_house<span class="token punctuation">::</span></span>hosting<span class="token punctuation">;</span>
  70. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">eat_at_restaurant</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  71. <span class="token namespace">hosting<span class="token punctuation">::</span></span><span class="token function">add_to_waitlist</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  72. <span class="token namespace">hosting<span class="token punctuation">::</span></span><span class="token function">add_to_waitlist</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  73. <span class="token namespace">hosting<span class="token punctuation">::</span></span><span class="token function">add_to_waitlist</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  74. <span class="token namespace">hosting<span class="token punctuation">::</span></span><span class="token function">some_function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">//报错,因为some_function为私有</span>
  75. <span class="token punctuation">}</span>
  76. </code></pre></div><p>使用 <code>use</code> 指定相对路径:<code>use front_of_house::hosting;</code></p> <p>可以直接将模块中的函数导入到作用域内,但是这样便无法区分该函数是在模块中定义还是在该文件内定义,因此通常的做法都是只导入到该函数的父模块,通过父模块调用函数。</p> <p>对于函数和同名条目是这样,但是对于结构体和枚举,通常是指定完整路径,这是 <code>use</code> 的习惯用法。</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span></span>fmt<span class="token punctuation">;</span>
  77. <span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span></span>io<span class="token punctuation">;</span>
  78. <span class="token keyword">fn</span> <span class="token function-definition function">f1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token namespace">fmt<span class="token punctuation">::</span></span><span class="token class-name">Result</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  79. <span class="token keyword">fn</span> <span class="token function-definition function">f2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token namespace">io<span class="token punctuation">::</span></span><span class="token class-name">Result</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  80. </code></pre></div><p><strong>as 关键字</strong></p> <p><code>as</code> 关键字可以为引入的路径指定一个本地的别名,例子如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span>fmt<span class="token punctuation">::</span></span><span class="token class-name">Result</span><span class="token punctuation">;</span>
  81. <span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span>io<span class="token punctuation">::</span></span><span class="token class-name">IOResult</span><span class="token punctuation">;</span>
  82. <span class="token keyword">fn</span> <span class="token function-definition function">f1</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Result</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  83. <span class="token keyword">fn</span> <span class="token function-definition function">f2</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">IOResult</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  84. </code></pre></div><p><strong>使用 pub use 重新导出名称</strong></p> <p>使用 use 将路径(名称)导入到作用域内后,该名称在此作用域内是私有的</p> <p>pub use:重导出</p> <ul><li>将条目引入作用域</li> <li>该条目可以被外部代码引入到它们的作用域</li></ul> <p><code>pub use crate::front_of_house::hosting;</code></p> <p><strong>使用外部包</strong></p> <ol><li>Cargo.toml 添加依赖的包(package)
  85. <ul><li>https://crates.io/</li></ul></li> <li>use 将特定条目引入作用域</li></ol> <p>标准库(std)也被当做外部包,但是不需要修改 Cargo.toml 来包含 std,需要使用 use 将 std 中的特定条目引入当前作用域</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span>collections<span class="token punctuation">::</span></span><span class="token class-name">HashMap</span><span class="token punctuation">;</span>
  86. </code></pre></div><p><strong>使用嵌套路径清理大量 use 语句</strong></p> <p>如果使用同一个包或模块下的多个条目,可以使用嵌套路径在同一行内将上述路径进行引入</p> <p>格式如下:<code>路径相同的部分::{路径不同的部分}</code>,例子如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span>cmp<span class="token punctuation">::</span></span><span class="token class-name">Ordering</span><span class="token punctuation">;</span>
  87. <span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span></span>io<span class="token punctuation">;</span>
  88. <span class="token comment">//使用嵌套路径</span>
  89. <span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span></span><span class="token punctuation">{</span><span class="token namespace">cmp<span class="token punctuation">::</span></span><span class="token class-name">Ordering</span><span class="token punctuation">,</span> io<span class="token punctuation">}</span><span class="token punctuation">;</span>
  90. <span class="token comment">//若是这样的引用</span>
  91. <span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span></span>io<span class="token punctuation">;</span>
  92. <span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span>io<span class="token punctuation">::</span></span><span class="token class-name">Write</span><span class="token punctuation">;</span>
  93. <span class="token comment">//简写为</span>
  94. <span class="token keyword">use</span> <span class="token namespace">std<span class="token punctuation">::</span>io<span class="token punctuation">::</span></span><span class="token punctuation">{</span><span class="token keyword">self</span><span class="token punctuation">,</span> <span class="token class-name">Write</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
  95. </code></pre></div><p><strong>通配符*</strong></p> <p>使用 <code>*</code> 可以把路径中所有的公共条目都引入到作用域</p> <p><code>use std::collections::*;</code></p> <p>应用场景:</p> <ul><li>测试。将所有被测试代码引入到 tests 模块</li> <li>有时被用于预导入(prelude)模块</li></ul> <h2 id="_4-将模块拆分为不同文件"><a href="#_4-将模块拆分为不同文件" class="header-anchor">#</a> 4. 将模块拆分为不同文件</h2> <p><strong>将模块内容移动到其它文件</strong></p> <p>模块定义时,如果模块名后边是 <code>;</code>,而不是代码块,Rust 会从与模块同名的文件中加载内容,同时项目文件夹结构要与模块层级结构一致。随着模块的变大,该技术可以把模块中的内容移动到其他文件中。</p> <p>例如:文件 <code>main.rs</code>:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">back_of_house</span><span class="token punctuation">;</span>
  96. </code></pre></div><p>文件 <code>back_of_house.rs</code>:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">hosting</span><span class="token punctuation">;</span>
  97. </code></pre></div><p>这时必须在 <code>src</code> 下创建一个 <code>back_of_house</code> 的文件夹,里面创建一个 <code>hosting.rs</code> 文件,即为 <code>src/back_of_house/hosting.rs</code></p> <p>文件 <code>hosting.rs</code>:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">test</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
  98. </code></pre></div></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
  99. <script src="/rust_camp_tutorial/assets/js/app.d7ab8f65.js" defer></script><script src="/rust_camp_tutorial/assets/js/2.3dc1b8de.js" defer></script><script src="/rust_camp_tutorial/assets/js/1.7f771cfb.js" defer></script><script src="/rust_camp_tutorial/assets/js/26.606cfbc8.js" defer></script>
  100. </body>
  101. </html>