Rust入门教程(五).html 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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/29.802642cf.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/26.606cfbc8.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/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%BA%94%EF%BC%89.html#一、panic" class="sidebar-link">一、panic!</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><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%BA%94%EF%BC%89.html#_1-1-rust-错误处理概述" class="sidebar-link">1.1 Rust 错误处理概述</a></li><li class="sidebar-sub-header"><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%BA%94%EF%BC%89.html#_1-2-不可恢复的错误与-panic" class="sidebar-link">1.2 不可恢复的错误与 panic!</a></li></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%BA%94%EF%BC%89.html#二、result-和可恢复的错误" class="sidebar-link">二、Result 和可恢复的错误</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><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%BA%94%EF%BC%89.html#_2-1-result-枚举" class="sidebar-link">2.1 Result 枚举</a></li><li class="sidebar-sub-header"><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%BA%94%EF%BC%89.html#_2-2-unwrap-与-expect" class="sidebar-link">2.2 unwrap 与 expect</a></li><li class="sidebar-sub-header"><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%BA%94%EF%BC%89.html#_2-3-传播错误" class="sidebar-link">2.3 传播错误</a></li><li class="sidebar-sub-header"><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%BA%94%EF%BC%89.html#_2-4-什么时候应该使用-panic" class="sidebar-link">2.4 什么时候应该使用 panic!</a></li></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> <h2 id="一、panic"><a href="#一、panic" class="header-anchor">#</a> 一、panic!</h2> <h3 id="_1-1-rust-错误处理概述"><a href="#_1-1-rust-错误处理概述" class="header-anchor">#</a> 1.1 Rust 错误处理概述</h3> <ul><li>Rust 的可靠性:错误处理
  19. <ul><li>大部分情况下:在编译时提示错误,并处理</li></ul></li> <li>错误的分类
  20. <ul><li>可恢复
  21. <ul><li>例如文件未找到,可再次尝试</li></ul></li> <li>不可恢复
  22. <ul><li>bug,例如访问的索引超出范围</li></ul></li></ul></li> <li>Rust 没有类似异常的机制
  23. <ul><li>可恢复错误:<code>Result&lt;T, E&gt;</code></li> <li>不可恢复:panic! 宏</li></ul></li></ul> <h3 id="_1-2-不可恢复的错误与-panic"><a href="#_1-2-不可恢复的错误与-panic" class="header-anchor">#</a> 1.2 不可恢复的错误与 panic!</h3> <ul><li>当 panic! 宏执行
  24. <ul><li>你的程序会打印一个错误信息</li> <li>展开(unwind)、清理调用栈(Stack)</li> <li>退出程序</li></ul></li></ul> <p><strong>为应对 panic,展开或中止(abort)调用栈</strong></p> <ul><li>默认情况下,当 panic 发生
  25. <ul><li>程序展开调用栈(工作量大)
  26. <ul><li>Rust 沿着调用栈往回走</li> <li>清理每个遇到的函数中的数据</li></ul></li> <li>或立即中止调用栈
  27. <ul><li>不进行清理,直接停止程序</li> <li>内存需要 OS 进行清理</li></ul></li></ul></li> <li>想让二进制文件更小,把设置从“展开”改为“中止”
  28. <ul><li>在 Cargo.toml 中适当的 profile 部分设置:</li> <li><code>panic = 'abort'</code></li></ul></li></ul> <p><strong>使用 panic! 产生的回溯信息</strong></p> <ul><li>panic!可能出现在
  29. <ul><li>我们写的代码中</li> <li>我们所依赖的代码中</li></ul></li> <li>可通过调用 panic! 的函数的回溯信息来定位引起问题的代码</li> <li>通过设置环境变量 <code>RUST_BACKTRACE</code> 可得到回溯信息
  30. <ul><li>Windows 下:<code>set RUST_BACKTRACE=1 &amp;&amp; cargo run</code></li> <li>Unix 系下:<code>RUST_BACKTRACE=1 cargo run</code></li></ul></li> <li>为了获取带有调试信息的回溯,必须启用调试符号(不带 <code>--release</code>)</li></ul> <h2 id="二、result-和可恢复的错误"><a href="#二、result-和可恢复的错误" class="header-anchor">#</a> 二、Result 和可恢复的错误</h2> <h3 id="_2-1-result-枚举"><a href="#_2-1-result-枚举" class="header-anchor">#</a> 2.1 Result 枚举</h3> <p>Result 枚举类型的定义:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">enum</span> <span class="token type-definition class-name">Result</span><span class="token operator">&lt;</span><span class="token class-name">T</span><span class="token punctuation">,</span> <span class="token class-name">E</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  31. <span class="token class-name">Ok</span><span class="token punctuation">(</span><span class="token class-name">T</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  32. <span class="token class-name">Err</span><span class="token punctuation">(</span><span class="token class-name">E</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
  33. <span class="token punctuation">}</span>
  34. </code></pre></div><p>T:操作成功情况下 Ok 变体里返回的数据的类型
  35. E:操作失败情况下 Err 变体里返回的错误的类型</p> <p>处理 Result 的一种方式:match 表达式。和 Option 枚举一样,Result 及其变体也是由 prelude 带入作用域,例子如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">test02</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  36. <span class="token keyword">let</span> file <span class="token operator">=</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  37. <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token keyword">match</span> file <span class="token punctuation">{</span>
  38. <span class="token class-name">Ok</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> file<span class="token punctuation">,</span>
  39. <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  40. <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;Open File Error: {:?}&quot;</span><span class="token punctuation">,</span> error<span class="token punctuation">)</span><span class="token punctuation">;</span>
  41. <span class="token punctuation">}</span>
  42. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  43. <span class="token punctuation">}</span>
  44. </code></pre></div><p>我们鼠标悬停在 file 变量上,可以看到它的类型是:<code>std::result::Result&lt;std::fs::File, std::io::Error&gt;</code>,说明 open 函数返回的是一个 Result 枚举,且其第一个参数就是该文件,第二个参数是 io 下的 Error 类型,包含了错误的具体信息。</p> <p>最终输出结果如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code>➜ ~<span class="token operator">/</span><span class="token class-name">Code</span><span class="token operator">/</span>rust<span class="token operator">/</span>panic git<span class="token punctuation">:</span><span class="token punctuation">(</span>master<span class="token punctuation">)</span> ✗ cargo run
  45. <span class="token class-name">Compiling</span> panic v0<span class="token number">.1</span><span class="token punctuation">.</span><span class="token number">0</span> <span class="token punctuation">(</span><span class="token operator">/</span>home<span class="token operator">/</span>cherry<span class="token operator">/</span><span class="token class-name">Code</span><span class="token operator">/</span>rust<span class="token operator">/</span>panic<span class="token punctuation">)</span>
  46. warning<span class="token punctuation">:</span> unused variable<span class="token punctuation">:</span> `f`
  47. <span class="token operator">-</span><span class="token punctuation">-&gt;</span> src<span class="token operator">/</span>main<span class="token punctuation">.</span>rs<span class="token punctuation">:</span><span class="token number">17</span><span class="token punctuation">:</span><span class="token number">9</span>
  48. <span class="token operator">|</span>
  49. <span class="token number">17</span> <span class="token operator">|</span> <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token keyword">match</span> file <span class="token punctuation">{</span>
  50. <span class="token operator">|</span> <span class="token operator">^</span> help<span class="token punctuation">:</span> <span class="token keyword">if</span> this is intentional<span class="token punctuation">,</span> prefix it with an underscore<span class="token punctuation">:</span> `_f`
  51. warning<span class="token punctuation">:</span> `panic` <span class="token punctuation">(</span>bin <span class="token string">&quot;panic&quot;</span><span class="token punctuation">)</span> generated <span class="token number">2</span> warnings
  52. <span class="token class-name">Finished</span> dev <span class="token punctuation">[</span>unoptimized <span class="token operator">+</span> debuginfo<span class="token punctuation">]</span> <span class="token function">target</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>46s
  53. <span class="token class-name">Running</span> `target<span class="token operator">/</span>debug<span class="token operator">/</span>panic`
  54. thread <span class="token lifetime-annotation symbol">'main</span>' panicked at <span class="token lifetime-annotation symbol">'Open</span> <span class="token class-name">File</span> <span class="token class-name">Error</span><span class="token punctuation">:</span> <span class="token class-name">Os</span> <span class="token punctuation">{</span> code<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> kind<span class="token punctuation">:</span> <span class="token class-name">NotFound</span><span class="token punctuation">,</span> message<span class="token punctuation">:</span> <span class="token string">&quot;No such file or directory&quot;</span> <span class="token punctuation">}</span>'<span class="token punctuation">,</span> src<span class="token operator">/</span>main<span class="token punctuation">.</span>rs<span class="token punctuation">:</span><span class="token number">20</span><span class="token punctuation">:</span><span class="token number">13</span>
  55. note<span class="token punctuation">:</span> run with `<span class="token constant">RUST_BACKTRACE</span><span class="token operator">=</span><span class="token number">1</span>` environment variable to display a backtrace
  56. </code></pre></div><p><strong>匹配不同的错误</strong></p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">test03</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  57. <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token keyword">match</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  58. <span class="token class-name">Ok</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> file<span class="token punctuation">,</span>
  59. <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">match</span> error<span class="token punctuation">.</span><span class="token function">kind</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{</span>
  60. <span class="token class-name">ErrorKind</span><span class="token punctuation">::</span><span class="token class-name">NotFound</span> <span class="token operator">=&gt;</span> <span class="token keyword">match</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  61. <span class="token class-name">Ok</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> file<span class="token punctuation">,</span>
  62. <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;Creating File Error: {:?}&quot;</span><span class="token punctuation">,</span> error<span class="token punctuation">)</span>
  63. <span class="token punctuation">}</span>
  64. other_error <span class="token operator">=&gt;</span> <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;Open File Error: {:?}&quot;</span><span class="token punctuation">,</span> other_error<span class="token punctuation">)</span>
  65. <span class="token punctuation">}</span>
  66. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  67. <span class="token punctuation">}</span>
  68. </code></pre></div><p>在 Err 中也会有很多种类型的错误,我们尝试匹配不同的错误类型,例如 <code>NotFound</code>。</p> <p>这里使用了很多 match,尽管很有用,但是比较原始。我们可以使用 <strong>闭包(closure)</strong> ,Result&lt;T, E&gt; 有很多方法,他们使用闭包作为参数,使用 match 进行实现,使用这些方法会使得代码更简洁</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">test04</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  69. <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap_or_else</span><span class="token punctuation">(</span><span class="token closure-params"><span class="token closure-punctuation punctuation">|</span>error<span class="token closure-punctuation punctuation">|</span></span> <span class="token punctuation">{</span>
  70. <span class="token keyword">if</span> error<span class="token punctuation">.</span><span class="token function">kind</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token class-name">ErrorKind</span><span class="token punctuation">::</span><span class="token class-name">NotFound</span> <span class="token punctuation">{</span>
  71. <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">create</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap_or_else</span><span class="token punctuation">(</span><span class="token closure-params"><span class="token closure-punctuation punctuation">|</span>error<span class="token closure-punctuation punctuation">|</span></span> <span class="token punctuation">{</span>
  72. <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;Creating File Error: {:?}&quot;</span><span class="token punctuation">,</span> error<span class="token punctuation">)</span><span class="token punctuation">;</span>
  73. <span class="token punctuation">}</span><span class="token punctuation">)</span>
  74. <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
  75. <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;Open File Error: {:?}&quot;</span><span class="token punctuation">,</span> error<span class="token punctuation">)</span><span class="token punctuation">;</span>
  76. <span class="token punctuation">}</span>
  77. <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  78. <span class="token punctuation">}</span>
  79. </code></pre></div><p>具体内容到后面再讲。</p> <h3 id="_2-2-unwrap-与-expect"><a href="#_2-2-unwrap-与-expect" class="header-anchor">#</a> 2.2 unwrap 与 expect</h3> <p><strong>unwrap</strong></p> <p>unwrap 是 match 表达式的一个快捷方法,如果 Result 结果是 Ok 则返回 Ok 里面的值,如果 Result 结果是 Err 则调用 panic!宏。以刚刚这段代码举例:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">test02</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  80. <span class="token keyword">let</span> file <span class="token operator">=</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo.txt&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  81. <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token keyword">match</span> file <span class="token punctuation">{</span>
  82. <span class="token class-name">Ok</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> file<span class="token punctuation">,</span>
  83. <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token punctuation">{</span>
  84. <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;Open File Error: {:?}&quot;</span><span class="token punctuation">,</span> error<span class="token punctuation">)</span><span class="token punctuation">;</span>
  85. <span class="token punctuation">}</span>
  86. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  87. <span class="token punctuation">}</span>
  88. </code></pre></div><p>unwrap 的作用类似于上面这段代码,当成功打开文件时,unwrap 就会返回 Ok 里面的值,否则就会调用 Err 代码块的代码,上面那段代码用 unwrap 就可以这样写:<code>let f = File::open(&quot;foo.txt&quot;).unwrap();</code></p> <p>但是发生恐慌的信息不可以自定义,这也是 unwrap 的一个缺点,而 Rust 提供了另一个方法:expect。</p> <p><strong>expect</strong></p> <p>和 unwrap 类似,但是可以指定错误信息:<code>let f = File::open(&quot;foo&quot;).expect(&quot;Open File Error!!!&quot;);</code>,这样得到的报错信息如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code>➜ ~<span class="token operator">/</span><span class="token class-name">Code</span><span class="token operator">/</span>rust<span class="token operator">/</span>panic git<span class="token punctuation">:</span><span class="token punctuation">(</span>master<span class="token punctuation">)</span> ✗ cargo run
  89. <span class="token class-name">Compiling</span> panic v0<span class="token number">.1</span><span class="token punctuation">.</span><span class="token number">0</span> <span class="token punctuation">(</span><span class="token operator">/</span>home<span class="token operator">/</span>cherry<span class="token operator">/</span><span class="token class-name">Code</span><span class="token operator">/</span>rust<span class="token operator">/</span>panic<span class="token punctuation">)</span>
  90. warning<span class="token punctuation">:</span> unused variable<span class="token punctuation">:</span> `f`
  91. <span class="token operator">-</span><span class="token punctuation">-&gt;</span> src<span class="token operator">/</span>main<span class="token punctuation">.</span>rs<span class="token punctuation">:</span><span class="token number">26</span><span class="token punctuation">:</span><span class="token number">9</span>
  92. <span class="token operator">|</span>
  93. <span class="token number">26</span> <span class="token operator">|</span> <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">expect</span><span class="token punctuation">(</span><span class="token string">&quot;Open File Error!!!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  94. <span class="token operator">|</span> <span class="token operator">^</span> help<span class="token punctuation">:</span> <span class="token keyword">if</span> this is intentional<span class="token punctuation">,</span> prefix it with an underscore<span class="token punctuation">:</span> `_f`
  95. warning<span class="token punctuation">:</span> `panic` <span class="token punctuation">(</span>bin <span class="token string">&quot;panic&quot;</span><span class="token punctuation">)</span> generated <span class="token number">1</span> warnings
  96. <span class="token class-name">Finished</span> dev <span class="token punctuation">[</span>unoptimized <span class="token operator">+</span> debuginfo<span class="token punctuation">]</span> <span class="token function">target</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">.</span>22s
  97. <span class="token class-name">Running</span> `target<span class="token operator">/</span>debug<span class="token operator">/</span>panic`
  98. thread <span class="token lifetime-annotation symbol">'main</span>' panicked at <span class="token lifetime-annotation symbol">'Open</span> <span class="token class-name">File</span> <span class="token macro property">Error!</span><span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">:</span> <span class="token class-name">Os</span> <span class="token punctuation">{</span> code<span class="token punctuation">:</span> <span class="token number">2</span><span class="token punctuation">,</span> kind<span class="token punctuation">:</span> <span class="token class-name">NotFound</span><span class="token punctuation">,</span> message<span class="token punctuation">:</span> <span class="token string">&quot;No such file or directory&quot;</span> <span class="token punctuation">}</span>'<span class="token punctuation">,</span> src<span class="token operator">/</span>main<span class="token punctuation">.</span>rs<span class="token punctuation">:</span><span class="token number">26</span><span class="token punctuation">:</span><span class="token number">31</span>
  99. note<span class="token punctuation">:</span> run with `<span class="token constant">RUST_BACKTRACE</span><span class="token operator">=</span><span class="token number">1</span>` environment variable to display a backtrace
  100. </code></pre></div><h3 id="_2-3-传播错误"><a href="#_2-3-传播错误" class="header-anchor">#</a> 2.3 传播错误</h3> <p>当写的函数中包含可能会执行失败的调用的时候,除了可以在函数中处理这个错误,还可以将错误返回给函数的调用者,让他们来决定如何进一步处理这个错误,这就叫做 <strong>传播错误</strong></p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">read_text_from_file</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 operator">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Error</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  101. <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  102. <span class="token keyword">let</span> <span class="token keyword">mut</span> f <span class="token operator">=</span> <span class="token keyword">match</span> f <span class="token punctuation">{</span>
  103. <span class="token class-name">Ok</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> file<span class="token punctuation">,</span>
  104. <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">return</span> <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
  105. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  106. <span class="token keyword">let</span> <span class="token keyword">mut</span> s <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  107. <span class="token keyword">match</span> f<span class="token punctuation">.</span><span class="token function">read_to_string</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> s<span class="token punctuation">)</span> <span class="token punctuation">{</span>
  108. <span class="token class-name">Ok</span><span class="token punctuation">(</span>_<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token class-name">Ok</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span><span class="token punctuation">,</span>
  109. <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token class-name">Err</span><span class="token punctuation">(</span>error<span class="token punctuation">)</span>
  110. <span class="token punctuation">}</span>
  111. <span class="token punctuation">}</span>
  112. <span class="token keyword">fn</span> <span class="token function-definition function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  113. <span class="token keyword">let</span> result <span class="token operator">=</span> <span class="token function">read_text_from_file</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  114. <span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;{:?}&quot;</span><span class="token punctuation">,</span> result<span class="token punctuation">)</span><span class="token punctuation">;</span>
  115. <span class="token punctuation">}</span>
  116. </code></pre></div><p>将 Result&lt;T, E&gt; 设置成函数返回值,这样就将错误传递给了调用者,若文件 foo 存在的话,最终便可以输出文件中的内容。</p> <p>Rust 中还提供了 <code>?</code> 运算符,用其来简化传播错误的操作。</p> <p>如果 Result 是 Ok:Ok 中的值就是表达式的结果,然后继续执行程序;
  117. 如果 Result 是 Err:Err 就是 <strong>整个函数</strong> 的返回值,就像使用了 return。例子如下:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">read_text_from_file_easy</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 operator">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Error</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  118. <span class="token keyword">let</span> <span class="token keyword">mut</span> f <span class="token operator">=</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">;</span>
  119. <span class="token keyword">let</span> <span class="token keyword">mut</span> s <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  120. f<span class="token punctuation">.</span><span class="token function">read_to_string</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> s<span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">;</span>
  121. <span class="token class-name">Ok</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span>
  122. <span class="token punctuation">}</span>
  123. </code></pre></div><p>上面这段简化后的代码的含义就是,若 <code>?</code> 前 Result 类型的值是 Ok,那么 Ok 里的值就会作为表达式的返回值进行返回,若类型是 Err,那么 Err 就当做整个函数的返回值进行返回。而 <code>f.read_to_string(&amp;mut s)?;</code> 中,若 Result 类型是 Ok,实际上里面值为空,没有用到,因此当表达式返回 Ok 后,返回一个 Ok(s) 作为函数的返回值,若类型为 Err,则将其作为函数返回值进行返回。</p> <p>上面这个例子还可以继续进行优化,使用链式调用:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">read_text_from_file_easist</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 operator">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Error</span><span class="token operator">&gt;</span> <span class="token punctuation">{</span>
  124. <span class="token keyword">let</span> <span class="token keyword">mut</span> s <span class="token operator">=</span> <span class="token class-name">String</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  125. <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">.</span><span class="token function">read_to_string</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">mut</span> s<span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">;</span>
  126. <span class="token class-name">Ok</span><span class="token punctuation">(</span>s<span class="token punctuation">)</span>
  127. <span class="token punctuation">}</span>
  128. </code></pre></div><p><strong>值得注意的是,要使用 ? 运算符,必须保证函数返回类型为 Result&lt;T, E&gt;</strong>,倘若我们尝试一下函数返回类型不是 Result,将会得到这样一条报错信息:<code>error[E0277]: the '?' operator can only be used in a function that returns 'Result' or 'Option' (or another type that implements 'FromResidual')</code></p> <p>因此,? 运算符只能用于返回类型为 Result 或 Option 的函数</p> <p><strong>? 运算符与 main 函数</strong></p> <ul><li>main 函数返回类型是:()</li> <li>main 函数的返回类型也可以是:Result&lt;T,E&gt;</li></ul> <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>error<span class="token punctuation">::</span></span><span class="token class-name">Error</span> <span class="token keyword">as</span> error<span class="token punctuation">;</span>
  129. <span class="token keyword">fn</span> <span class="token function-definition function">main</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 operator">&lt;</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name">Box</span><span class="token operator">&lt;</span><span class="token keyword">dyn</span> error<span class="token operator">&gt;&gt;</span> <span class="token punctuation">{</span>
  130. <span class="token keyword">let</span> f <span class="token operator">=</span> <span class="token class-name">File</span><span class="token punctuation">::</span><span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;foo&quot;</span><span class="token punctuation">)</span><span class="token operator">?</span><span class="token punctuation">;</span>
  131. <span class="token class-name">Ok</span><span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
  132. <span class="token punctuation">}</span>
  133. </code></pre></div><p><code>Box&lt;dyn Error&gt;</code> 是 trait 对象,可以简单理解为“任何可能的错误类型”。</p> <p>这样就可以在 main 函数中使用 <code>?</code> 运算符了。</p> <p>(开始玄学 o_o)</p> <p><strong>? 与 from 函数</strong></p> <ul><li><code>Trait std:convert::From</code> 上的 from 函数
  134. <ul><li>用于错误之间的转换</li></ul></li> <li>被 ? 所应用的错误,会隐式的被 from 函数处理</li> <li>当 ? 调用 from 函数时
  135. <ul><li>它所接收的错误类型会被转化为当前函数返回类型所定义的错误类型</li></ul></li> <li>用于:针对不同错误原因,返回同一种错误类型
  136. <ul><li>只要每个错误类型实现了转换为所返回的错误类型的 from 函数</li></ul></li></ul> <h3 id="_2-4-什么时候应该使用-panic"><a href="#_2-4-什么时候应该使用-panic" class="header-anchor">#</a> 2.4 什么时候应该使用 panic!</h3> <p><strong>总体原则</strong></p> <p>在定义一个可能失败的函数时,优先考虑返回 Result,若你觉得这个错误一定无法恢复,那就可以代替调用者调用 panic!</p> <p><strong>编写示例、原型代码、测试</strong></p> <p>可以使用panic!</p> <ul><li>演示某些概念: unwrap</li> <li>原型代码: unwrap、expect</li> <li>测试: unwrap、expect
  137. <ul><li>测试的失败是用 panic! 进行标记的</li></ul></li></ul> <p><strong>有时你比编译器掌握更多的信息</strong></p> <p>你可以确定 Result 就是 Ok,那么可以使用 unwrap,例子如下:</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>net<span class="token punctuation">::</span></span><span class="token class-name">IpAddr</span><span class="token punctuation">;</span>
  138. <span class="token keyword">fn</span> <span class="token function-definition function">test06</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  139. <span class="token keyword">let</span> home<span class="token punctuation">:</span> <span class="token class-name">IpAddr</span> <span class="token operator">=</span> <span class="token string">&quot;192.168.3.110&quot;</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  140. <span class="token punctuation">}</span>
  141. </code></pre></div><p>这里我们可以确定这个 IP 地址解析出来一定是有效的,因此可以直接使用 unwrap。</p> <p><strong>错误处理的指导性建议</strong></p> <ul><li>当代码最终可能处于损坏状态时,最好使用 panic!</li> <li>损坏状态(Bad state):某些假设、保证、约定或不可变性被打破
  142. <ul><li>例如非法的值、矛盾的值或空缺的值被传入代码</li> <li>以及下列中的一条:
  143. <ul><li>这种损坏状态并不是预期能够偶尔发生的事情</li> <li>在此之后,您的代码如果处于这种损坏状态就无法运行</li> <li>在您使用的类型中没有一个好的方法来将这些信息(处于损坏状态)进行编码</li></ul></li></ul></li></ul> <p><strong>场景建议</strong></p> <ul><li>调用你的代码,传入无意义的参数值:panic!</li> <li>调用外部不可控代码,返回非法状态,你无法修复:panic!</li> <li>如果失败是可预期的:Result</li> <li>当你的代码对值进行操作,首先应该验证这些值:panic!</li></ul> <p><strong>为验证创建自定义类型</strong></p> <p>创建新的类型,把验证逻辑放在构造实例的函数里。</p> <p>以第一节的猜数游戏为例:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">fn</span> <span class="token function-definition function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  144. <span class="token keyword">loop</span> <span class="token punctuation">{</span>
  145. <span class="token comment">//...</span>
  146. <span class="token keyword">let</span> guess <span class="token operator">=</span> <span class="token string">&quot;32&quot;</span><span class="token punctuation">;</span>
  147. <span class="token keyword">let</span> guess<span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token keyword">match</span> guess<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  148. <span class="token class-name">Ok</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> num<span class="token punctuation">,</span>
  149. <span class="token class-name">Err</span><span class="token punctuation">(</span>_<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">continue</span><span class="token punctuation">,</span>
  150. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  151. <span class="token keyword">if</span> guess <span class="token operator">&lt;</span> <span class="token number">1</span> <span class="token operator">||</span> guess <span class="token operator">&gt;</span> <span class="token number">100</span> <span class="token punctuation">{</span>
  152. <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;The num must between 1 and 100&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
  153. <span class="token keyword">continue</span><span class="token punctuation">;</span>
  154. <span class="token punctuation">}</span>
  155. <span class="token comment">//...</span>
  156. <span class="token punctuation">}</span>
  157. <span class="token punctuation">}</span>
  158. </code></pre></div><p>这样一个功能就是判断输入的数是否符合 i32 类型,若符合的话表达式返回 num,然后再判断是否在 1~100 之间,如果不满足则继续循环。如果有多个函数中都需要类似这样的判断,则代码便会显得冗余,我们可以自定义一个验证逻辑:</p> <div class="language-rust extra-class"><pre class="language-rust"><code><span class="token keyword">pub</span> <span class="token keyword">struct</span> <span class="token type-definition class-name">Guess</span> <span class="token punctuation">{</span>
  159. value<span class="token punctuation">:</span> <span class="token keyword">i32</span>
  160. <span class="token punctuation">}</span>
  161. <span class="token keyword">impl</span> <span class="token class-name">Guess</span> <span class="token punctuation">{</span>
  162. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">new</span><span class="token punctuation">(</span>value<span class="token punctuation">:</span> <span class="token keyword">i32</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token class-name">Guess</span> <span class="token punctuation">{</span>
  163. <span class="token keyword">if</span> value <span class="token operator">&lt;</span> <span class="token number">1</span> <span class="token operator">||</span> value <span class="token operator">&gt;</span> <span class="token number">100</span> <span class="token punctuation">{</span>
  164. <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;The guess value must between 1 and 100, got {}&quot;</span><span class="token punctuation">,</span> value<span class="token punctuation">)</span><span class="token punctuation">;</span>
  165. <span class="token punctuation">}</span>
  166. <span class="token class-name">Guess</span> <span class="token punctuation">{</span>value<span class="token punctuation">}</span>
  167. <span class="token punctuation">}</span>
  168. <span class="token comment">//类似 getter 方法</span>
  169. <span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">value</span><span class="token punctuation">(</span><span class="token operator">&amp;</span><span class="token keyword">self</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i32</span> <span class="token punctuation">{</span>
  170. <span class="token keyword">self</span><span class="token punctuation">.</span>value
  171. <span class="token punctuation">}</span>
  172. <span class="token punctuation">}</span>
  173. <span class="token keyword">fn</span> <span class="token function-definition function">guess_game</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  174. <span class="token keyword">loop</span> <span class="token punctuation">{</span>
  175. <span class="token comment">//...</span>
  176. <span class="token keyword">let</span> guess <span class="token operator">=</span> <span class="token string">&quot;32&quot;</span><span class="token punctuation">;</span>
  177. <span class="token keyword">let</span> guess<span class="token punctuation">:</span> <span class="token keyword">i32</span> <span class="token operator">=</span> <span class="token keyword">match</span> guess<span class="token punctuation">.</span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">parse</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
  178. <span class="token class-name">Ok</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> num<span class="token punctuation">,</span>
  179. <span class="token class-name">Err</span><span class="token punctuation">(</span>_<span class="token punctuation">)</span> <span class="token operator">=&gt;</span> <span class="token keyword">continue</span><span class="token punctuation">,</span>
  180. <span class="token punctuation">}</span><span class="token punctuation">;</span>
  181. <span class="token keyword">let</span> guess <span class="token operator">=</span> <span class="token class-name">Guess</span><span class="token punctuation">::</span><span class="token function">new</span><span class="token punctuation">(</span>guess<span class="token punctuation">)</span><span class="token punctuation">;</span>
  182. <span class="token punctuation">}</span>
  183. <span class="token punctuation">}</span>
  184. </code></pre></div><p>如果能够成功创建 Guess 实例的话,那么就说明值通过了验证,而不需要将验证功能写在函数里了。</p> <p>上述 value 方法是获得 Guess 结构体中的 value 字段值,因为结构体中的字段是私有的,外部无法直接对字段赋值。</p></div> <footer class="page-edit"><!----> <!----></footer> <!----> </main></div><div class="global-ui"></div></div>
  185. <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/29.802642cf.js" defer></script>
  186. </body>
  187. </html>