Skip to content

Commit b53a272

Browse files
authored
Merge pull request #173 from kdnakt/translate-read_lines
Translate untranslated lines in read_lines.md
2 parents 9919840 + eef931a commit b53a272

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/std_misc/file/read_lines.md

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
# `read_lines`
22

3+
<!--
34
## A naive approach
5+
-->
6+
## 単純なやり方
47

8+
<!--
59
This might be a reasonable first attempt for a beginner's first
610
implementation for reading lines from a file.
11+
-->
12+
テキストファイルの行を読み込むのを、初心者が初めて実装した場合、
13+
以下のようになるでしょう。
714

815
```rust,norun
916
use std::fs::read_to_string;
@@ -19,33 +26,57 @@ fn read_lines(filename: &str) -> Vec<String> {
1926
}
2027
```
2128

29+
<!--
2230
Since the method `lines()` returns an iterator over the lines in the file,
2331
we can also perform a map inline and collect the results, yielding a more
2432
concise and fluent expression.
33+
-->
34+
`lines()`メソッドはファイルの各行のイテレータを返すので、
35+
インラインでマップを実行し結果を収集することもできます。
36+
そうすると、より簡潔で読みやすい表現となります。
2537

2638
```rust,norun
2739
use std::fs::read_to_string;
2840
2941
fn read_lines(filename: &str) -> Vec<String> {
3042
read_to_string(filename)
3143
.unwrap() // panic on possible file-reading errors
44+
// ファイル読み込みエラーの場合はパニックする。
3245
.lines() // split the string into an iterator of string slices
46+
// 文字列のスライスのイテレータに分割する。
3347
.map(String::from) // make each slice into a string
48+
// スライスを文字列に変換する。
3449
.collect() // gather them together into a vector
50+
// ベクタにまとめる。
3551
}
3652
```
3753

54+
<!--
3855
Note that in both examples above, we must convert the `&str` reference
3956
returned from `lines()` to the owned type `String`, using `.to_string()`
4057
and `String::from` respectively.
58+
-->
59+
上の例では、`lines()`から返された`&str`
60+
それぞれ`to_string()``String::from`を使って、
61+
所有権のある`String`型に変換しなければならない点に注意してください。
4162

63+
<!--
4264
## A more efficient approach
65+
-->
66+
## より効率的なやり方
4367

68+
<!--
4469
Here we pass ownership of the open `File` to a `BufReader` struct. `BufReader` uses an internal
4570
buffer to reduce intermediate allocations.
71+
-->
72+
ここでは、開いた`File`の所有権を`BufReader`構造体に渡します。
73+
`BufReader`は内部的なバッファを使い、中間のメモリ割り当てを削減します。
4674

75+
<!--
4776
We also update `read_lines` to return an iterator instead of allocating new
4877
`String` objects in memory for each line.
78+
-->
79+
`read_lines`を更新して、それぞれの行に対してメモリ上に新しい`String`オブジェクトを割り当てるのではなく、イテレータを返すようにします。
4980

5081
```rust,no_run
5182
use std::fs::File;
@@ -54,8 +85,10 @@ use std::path::Path;
5485
5586
fn main() {
5687
// File hosts.txt must exist in the current path
88+
// hosts.txtファイルは現在のパスに存在しなければならない。
5789
if let Ok(lines) = read_lines("./hosts.txt") {
5890
// Consumes the iterator, returns an (Optional) String
91+
// イテレータを消費し、Option型のStringを返す。
5992
for line in lines {
6093
if let Ok(ip) = line {
6194
println!("{}", ip);
@@ -66,24 +99,37 @@ fn main() {
6699
67100
// The output is wrapped in a Result to allow matching on errors
68101
// Returns an Iterator to the Reader of the lines of the file.
102+
// 出力はResult型にラップされ、エラーをマッチできるようになる。
103+
// ファイルの各行のReaderへのイテレータを返す。
69104
fn read_lines<P>(filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
70105
where P: AsRef<Path>, {
71106
let file = File::open(filename)?;
72107
Ok(io::BufReader::new(file).lines())
73108
}
74109
```
75110

111+
<!--
76112
Running this program simply prints the lines individually.
113+
-->
114+
このプログラムを実行すると、単に各行をプリントします。
115+
77116
```shell
78117
$ echo -e "127.0.0.1\n192.168.0.1\n" > hosts.txt
79118
$ rustc read_lines.rs && ./read_lines
80119
127.0.0.1
81120
192.168.0.1
82121
```
83122

123+
<!--
84124
(Note that since `File::open` expects a generic `AsRef<Path>` as argument, we define our
85125
generic `read_lines()` method with the same generic constraint, using the `where` keyword.)
126+
-->
127+
`File::open`はジェネリックな`AsRef<Path>`を引数にとるので、
128+
ジェネリックな`read_lines`メソッドも、`where`キーワードを使って、同じジェネリックな制約を持たせています。
86129

130+
<!--
87131
This process is more efficient than creating a `String` in memory with all of the file's
88132
contents. This can especially cause performance issues when working with larger files.
89-
133+
-->
134+
この処理は、ファイルの中身全てをメモリ上の`String`にするよりも効率的です。
135+
メモリ上に`String`を作ると、より大きなファイルを取り扱う際に、パフォーマンスの問題につながります。

0 commit comments

Comments
 (0)