@@ -12612,9 +12612,16 @@ <h1 id="ファイル-io"><a class="header" href="#ファイル-io">ファイル
12612
12612
-->
12613
12613
<p><a href="https://doc.rust-lang.org/std/fs/struct.OpenOptions.html"><code>OpenOptions</code></a>構造体を利用して、ファイルの開き方を設定できます。</p>
12614
12614
<div style="break-before: page; page-break-before: always;"></div><h1 id="read_lines"><a class="header" href="#read_lines"><code>read_lines</code></a></h1>
12615
- <h2 id="a-naive-approach"><a class="header" href="#a-naive-approach">A naive approach</a></h2>
12616
- <p>This might be a reasonable first attempt for a beginner's first
12617
- implementation for reading lines from a file.</p>
12615
+ <!--
12616
+ ## A naive approach
12617
+ -->
12618
+ <h2 id="単純なやり方"><a class="header" href="#単純なやり方">単純なやり方</a></h2>
12619
+ <!--
12620
+ This might be a reasonable first attempt for a beginner's first
12621
+ implementation for reading lines from a file.
12622
+ -->
12623
+ <p>テキストファイルの行を読み込むのを、初心者が初めて実装した場合、
12624
+ 以下のようになるでしょう。</p>
12618
12625
<pre><pre class="playground"><code class="language-rust norun edition2021"><span class="boring">#![allow(unused)]
12619
12626
</span><span class="boring">fn main() {
12620
12627
</span>use std::fs::read_to_string;
@@ -12629,37 +12636,63 @@ <h2 id="a-naive-approach"><a class="header" href="#a-naive-approach">A naive app
12629
12636
result
12630
12637
}
12631
12638
<span class="boring">}</span></code></pre></pre>
12632
- <p>Since the method <code>lines()</code> returns an iterator over the lines in the file,
12639
+ <!--
12640
+ Since the method `lines()` returns an iterator over the lines in the file,
12633
12641
we can also perform a map inline and collect the results, yielding a more
12634
- concise and fluent expression.</p>
12642
+ concise and fluent expression.
12643
+ -->
12644
+ <p><code>lines()</code>メソッドはファイルの各行のイテレータを返すので、
12645
+ インラインでマップを実行し結果を収集することもできます。
12646
+ そうすると、より簡潔で読みやすい表現となります。</p>
12635
12647
<pre><pre class="playground"><code class="language-rust norun edition2021"><span class="boring">#![allow(unused)]
12636
12648
</span><span class="boring">fn main() {
12637
12649
</span>use std::fs::read_to_string;
12638
12650
12639
12651
fn read_lines(filename: &str) -> Vec<String> {
12640
12652
read_to_string(filename)
12641
12653
.unwrap() // panic on possible file-reading errors
12654
+ // ファイル読み込みエラーの場合はパニックする。
12642
12655
.lines() // split the string into an iterator of string slices
12656
+ // 文字列のスライスのイテレータに分割する。
12643
12657
.map(String::from) // make each slice into a string
12658
+ // スライスを文字列に変換する。
12644
12659
.collect() // gather them together into a vector
12660
+ // ベクタにまとめる。
12645
12661
}
12646
12662
<span class="boring">}</span></code></pre></pre>
12647
- <p>Note that in both examples above, we must convert the <code>&str</code> reference
12648
- returned from <code>lines()</code> to the owned type <code>String</code>, using <code>.to_string()</code>
12649
- and <code>String::from</code> respectively.</p>
12650
- <h2 id="a-more-efficient-approach"><a class="header" href="#a-more-efficient-approach">A more efficient approach</a></h2>
12651
- <p>Here we pass ownership of the open <code>File</code> to a <code>BufReader</code> struct. <code>BufReader</code> uses an internal
12652
- buffer to reduce intermediate allocations.</p>
12653
- <p>We also update <code>read_lines</code> to return an iterator instead of allocating new
12654
- <code>String</code> objects in memory for each line.</p>
12663
+ <!--
12664
+ Note that in both examples above, we must convert the `&str` reference
12665
+ returned from `lines()` to the owned type `String`, using `.to_string()`
12666
+ and `String::from` respectively.
12667
+ -->
12668
+ <p>上の例では、<code>lines()</code>から返された<code>&str</code>を
12669
+ それぞれ<code>to_string()</code>と<code>String::from</code>を使って、
12670
+ 所有権のある<code>String</code>型に変換しなければならない点に注意してください。</p>
12671
+ <!--
12672
+ ## A more efficient approach
12673
+ -->
12674
+ <h2 id="より効率的なやり方"><a class="header" href="#より効率的なやり方">より効率的なやり方</a></h2>
12675
+ <!--
12676
+ Here we pass ownership of the open `File` to a `BufReader` struct. `BufReader` uses an internal
12677
+ buffer to reduce intermediate allocations.
12678
+ -->
12679
+ <p>ここでは、開いた<code>File</code>の所有権を<code>BufReader</code>構造体に渡します。
12680
+ <code>BufReader</code>は内部的なバッファを使い、中間のメモリ割り当てを削減します。</p>
12681
+ <!--
12682
+ We also update `read_lines` to return an iterator instead of allocating new
12683
+ `String` objects in memory for each line.
12684
+ -->
12685
+ <p><code>read_lines</code>を更新して、それぞれの行に対してメモリ上に新しい<code>String</code>オブジェクトを割り当てるのではなく、イテレータを返すようにします。</p>
12655
12686
<pre><pre class="playground"><code class="language-rust no_run edition2021">use std::fs::File;
12656
12687
use std::io::{self, BufRead};
12657
12688
use std::path::Path;
12658
12689
12659
12690
fn main() {
12660
12691
// File hosts.txt must exist in the current path
12692
+ // hosts.txtファイルは現在のパスに存在しなければならない。
12661
12693
if let Ok(lines) = read_lines("./hosts.txt") {
12662
12694
// Consumes the iterator, returns an (Optional) String
12695
+ // イテレータを消費し、Option型のStringを返す。
12663
12696
for line in lines {
12664
12697
if let Ok(ip) = line {
12665
12698
println!("{}", ip);
@@ -12670,21 +12703,34 @@ <h2 id="a-more-efficient-approach"><a class="header" href="#a-more-efficient-app
12670
12703
12671
12704
// The output is wrapped in a Result to allow matching on errors
12672
12705
// Returns an Iterator to the Reader of the lines of the file.
12706
+ // 出力はResult型にラップされ、エラーをマッチできるようになる。
12707
+ // ファイルの各行のReaderへのイテレータを返す。
12673
12708
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
12674
12709
where P: AsRef<Path>, {
12675
12710
let file = File::open(filename)?;
12676
12711
Ok(io::BufReader::new(file).lines())
12677
12712
}</code></pre></pre>
12678
- <p>Running this program simply prints the lines individually.</p>
12713
+ <!--
12714
+ Running this program simply prints the lines individually.
12715
+ -->
12716
+ <p>このプログラムを実行すると、単に各行をプリントします。</p>
12679
12717
<pre><code class="language-shell">$ echo -e "127.0.0.1\n192.168.0.1\n" > hosts.txt
12680
12718
$ rustc read_lines.rs && ./read_lines
12681
12719
127.0.0.1
12682
12720
192.168.0.1
12683
12721
</code></pre>
12684
- <p>(Note that since <code>File::open</code> expects a generic <code>AsRef<Path></code> as argument, we define our
12685
- generic <code>read_lines()</code> method with the same generic constraint, using the <code>where</code> keyword.)</p>
12686
- <p>This process is more efficient than creating a <code>String</code> in memory with all of the file's
12687
- contents. This can especially cause performance issues when working with larger files.</p>
12722
+ <!--
12723
+ (Note that since `File::open` expects a generic `AsRef<Path>` as argument, we define our
12724
+ generic `read_lines()` method with the same generic constraint, using the `where` keyword.)
12725
+ -->
12726
+ <p><code>File::open</code>はジェネリックな<code>AsRef<Path></code>を引数にとるので、
12727
+ ジェネリックな<code>read_lines</code>メソッドも、<code>where</code>キーワードを使って、同じジェネリックな制約を持たせています。</p>
12728
+ <!--
12729
+ This process is more efficient than creating a `String` in memory with all of the file's
12730
+ contents. This can especially cause performance issues when working with larger files.
12731
+ -->
12732
+ <p>この処理は、ファイルの中身全てをメモリ上の<code>String</code>にするよりも効率的です。
12733
+ メモリ上に<code>String</code>を作ると、より大きなファイルを取り扱う際に、パフォーマンスの問題につながります。</p>
12688
12734
<div style="break-before: page; page-break-before: always;"></div><!--
12689
12735
# Child processes
12690
12736
-->
0 commit comments