@@ -7359,12 +7359,19 @@ <h1 id="部分的ムーブ"><a class="header" href="#部分的ムーブ">部分
7359
7359
// `person` は使用できないが、 `person.age` はムーブしていないので使用できる
7360
7360
println!("The person's age from person struct is {}", person.age);
7361
7361
}</code></pre></pre>
7362
- <p>(In this example, we store the <code>age</code> variable on the heap to
7363
- illustrate the partial move: deleting <code>ref</code> in the above code would
7364
- give an error as the ownership of <code>person.age</code> would be moved to the
7365
- variable <code>age</code>. If <code>Person.age</code> were stored on the stack, <code>ref</code> would
7366
- not be required as the definition of <code>age</code> would copy the data from
7367
- <code>person.age</code> without moving it.)</p>
7362
+ <!--
7363
+ (In this example, we store the `age` variable on the heap to
7364
+ illustrate the partial move: deleting `ref` in the above code would
7365
+ give an error as the ownership of `person.age` would be moved to the
7366
+ variable `age`. If `Person.age` were stored on the stack, `ref` would
7367
+ not be required as the definition of `age` would copy the data from
7368
+ `person.age` without moving it.)
7369
+ -->
7370
+ <p>この例では、<code>age</code>変数をヒープ上に保持し、部分的ムーブを説明しています。
7371
+ 上記コードで<code>ref</code>を削除すると、<code>person.age</code>の所有権が<code>age</code>変数にムーブされるため、エラーになります。
7372
+ もしも<code>Person.age</code>がスタック上に保持されていたら、
7373
+ <code>age</code>の定義が<code>person.age</code>をムーブすることなくデータをコピーするので、
7374
+ <code>ref</code>は必須ではないのですが、実際にはヒープ上に保持されているため<code>ref</code>は必須です。</p>
7368
7375
<!--
7369
7376
### See also:
7370
7377
-->
@@ -11595,11 +11602,18 @@ <h2 id="literals-and-escapes"><a class="header" href="#literals-and-escapes">Lit
11595
11602
println!("{}", op(1.0, 10.0));
11596
11603
}</code></pre></pre>
11597
11604
<div style="break-before: page; page-break-before: always;"></div><h1 id=""><a class="header" href="#"><code>?</code></a></h1>
11598
- <p>Chaining results using match can get pretty untidy; luckily, the <code>?</code> operator
11599
- can be used to make things pretty again. <code>?</code> is used at the end of an expression
11600
- returning a <code>Result</code>, and is equivalent to a match expression, where the
11601
- <code>Err(err)</code> branch expands to an early <code>return Err(From::from(err))</code>, and the <code>Ok(ok)</code>
11602
- branch expands to an <code>ok</code> expression.</p>
11605
+ <!--
11606
+ Chaining results using match can get pretty untidy; luckily, the `?` operator
11607
+ can be used to make things pretty again. `?` is used at the end of an expression
11608
+ returning a `Result`, and is equivalent to a match expression, where the
11609
+ `Err(err)` branch expands to an early `return Err(From::from(err))`, and the `Ok(ok)`
11610
+ branch expands to an `ok` expression.
11611
+ -->
11612
+ <p>マッチを利用して結果をチェインするのは中々面倒です。
11613
+ 幸いなことに、<code>?</code>マクロを使用すればイケてるコードに戻すことができます。
11614
+ <code>?</code>は<code>Result</code>を返す式の末尾で使います。
11615
+ <code>Err(err)</code>の分岐が<code>return Err(From::from(err))</code>という早期リターンに展開され、
11616
+ <code>Ok(ok)</code>の分岐が<code>ok</code>の式に展開されるようなマッチ式と等価です。</p>
11603
11617
<pre><pre class="playground"><code class="language-rust editable ignore mdbook-runnable edition2021">mod checked {
11604
11618
#[derive(Debug)]
11605
11619
enum MathError {
@@ -11635,11 +11649,14 @@ <h2 id="literals-and-escapes"><a class="header" href="#literals-and-escapes">Lit
11635
11649
}
11636
11650
11637
11651
// Intermediate function
11652
+ // 中間関数
11638
11653
fn op_(x: f64, y: f64) -> MathResult {
11639
11654
// if `div` "fails", then `DivisionByZero` will be `return`ed
11655
+ // `div`が"失敗"したら、`DivisionByZero`が`return`される。
11640
11656
let ratio = div(x, y)?;
11641
11657
11642
11658
// if `ln` "fails", then `NonPositiveLogarithm` will be `return`ed
11659
+ // もし`ln`が"失敗"したら、`NonPositiveLogarithm`が`return`される。
11643
11660
let ln = ln(ratio)?;
11644
11661
11645
11662
sqrt(ln)
@@ -11663,8 +11680,12 @@ <h2 id="literals-and-escapes"><a class="header" href="#literals-and-escapes">Lit
11663
11680
fn main() {
11664
11681
checked::op(1.0, 10.0);
11665
11682
}</code></pre></pre>
11666
- <p>Be sure to check the <a href="https://doc.rust-lang.org/std/result/index.html">documentation</a>,
11667
- as there are many methods to map/compose <code>Result</code>.</p>
11683
+ <!--
11684
+ Be sure to check the [documentation][docs],
11685
+ as there are many methods to map/compose `Result`.
11686
+ -->
11687
+ <p><a href="https://doc.rust-lang.org/std/result/index.html">公式ドキュメント</a>をチェックすることをオススメします。
11688
+ <code>Result</code>型を扱う関数や<code>Result</code>型のメソッドが多く挙げられています。</p>
11668
11689
<div style="break-before: page; page-break-before: always;"></div><h1 id="panic-1"><a class="header" href="#panic-1"><code>panic!</code></a></h1>
11669
11690
<!--
11670
11691
The `panic!` macro can be used to generate a panic and start unwinding
0 commit comments