Skip to content

Commit 9b5c9ee

Browse files
committed
ci: generate pages at b53a272 [ci skip]
1 parent b53a272 commit 9b5c9ee

File tree

4 files changed

+130
-38
lines changed

4 files changed

+130
-38
lines changed

docs/print.html

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12612,9 +12612,16 @@ <h1 id="ファイル-io"><a class="header" href="#ファイル-io">ファイル
1261212612
-->
1261312613
<p><a href="https://doc.rust-lang.org/std/fs/struct.OpenOptions.html"><code>OpenOptions</code></a>構造体を利用して、ファイルの開き方を設定できます。</p>
1261412614
<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>
1261812625
<pre><pre class="playground"><code class="language-rust norun edition2021"><span class="boring">#![allow(unused)]
1261912626
</span><span class="boring">fn main() {
1262012627
</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
1262912636
result
1263012637
}
1263112638
<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,
1263312641
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>
1263512647
<pre><pre class="playground"><code class="language-rust norun edition2021"><span class="boring">#![allow(unused)]
1263612648
</span><span class="boring">fn main() {
1263712649
</span>use std::fs::read_to_string;
1263812650

1263912651
fn read_lines(filename: &amp;str) -&gt; Vec&lt;String&gt; {
1264012652
read_to_string(filename)
1264112653
.unwrap() // panic on possible file-reading errors
12654+
// ファイル読み込みエラーの場合はパニックする。
1264212655
.lines() // split the string into an iterator of string slices
12656+
// 文字列のスライスのイテレータに分割する。
1264312657
.map(String::from) // make each slice into a string
12658+
// スライスを文字列に変換する。
1264412659
.collect() // gather them together into a vector
12660+
// ベクタにまとめる。
1264512661
}
1264612662
<span class="boring">}</span></code></pre></pre>
12647-
<p>Note that in both examples above, we must convert the <code>&amp;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>&amp;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>
1265512686
<pre><pre class="playground"><code class="language-rust no_run edition2021">use std::fs::File;
1265612687
use std::io::{self, BufRead};
1265712688
use std::path::Path;
1265812689

1265912690
fn main() {
1266012691
// File hosts.txt must exist in the current path
12692+
// hosts.txtファイルは現在のパスに存在しなければならない。
1266112693
if let Ok(lines) = read_lines(&quot;./hosts.txt&quot;) {
1266212694
// Consumes the iterator, returns an (Optional) String
12695+
// イテレータを消費し、Option型のStringを返す。
1266312696
for line in lines {
1266412697
if let Ok(ip) = line {
1266512698
println!(&quot;{}&quot;, ip);
@@ -12670,21 +12703,34 @@ <h2 id="a-more-efficient-approach"><a class="header" href="#a-more-efficient-app
1267012703

1267112704
// The output is wrapped in a Result to allow matching on errors
1267212705
// Returns an Iterator to the Reader of the lines of the file.
12706+
// 出力はResult型にラップされ、エラーをマッチできるようになる。
12707+
// ファイルの各行のReaderへのイテレータを返す。
1267312708
fn read_lines&lt;P&gt;(filename: P) -&gt; io::Result&lt;io::Lines&lt;io::BufReader&lt;File&gt;&gt;&gt;
1267412709
where P: AsRef&lt;Path&gt;, {
1267512710
let file = File::open(filename)?;
1267612711
Ok(io::BufReader::new(file).lines())
1267712712
}</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>
1267912717
<pre><code class="language-shell">$ echo -e &quot;127.0.0.1\n192.168.0.1\n&quot; &gt; hosts.txt
1268012718
$ rustc read_lines.rs &amp;&amp; ./read_lines
1268112719
127.0.0.1
1268212720
192.168.0.1
1268312721
</code></pre>
12684-
<p>(Note that since <code>File::open</code> expects a generic <code>AsRef&lt;Path&gt;</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&lt;Path&gt;</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>
1268812734
<div style="break-before: page; page-break-before: always;"></div><!--
1268912735
# Child processes
1269012736
-->

docs/searchindex.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/searchindex.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

docs/std_misc/file/read_lines.html

Lines changed: 64 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,16 @@ <h1 class="menu-title">Rust By Example 日本語版</h1>
146146
<div id="content" class="content">
147147
<main>
148148
<h1 id="read_lines"><a class="header" href="#read_lines"><code>read_lines</code></a></h1>
149-
<h2 id="a-naive-approach"><a class="header" href="#a-naive-approach">A naive approach</a></h2>
150-
<p>This might be a reasonable first attempt for a beginner's first
151-
implementation for reading lines from a file.</p>
149+
<!--
150+
## A naive approach
151+
-->
152+
<h2 id="単純なやり方"><a class="header" href="#単純なやり方">単純なやり方</a></h2>
153+
<!--
154+
This might be a reasonable first attempt for a beginner's first
155+
implementation for reading lines from a file.
156+
-->
157+
<p>テキストファイルの行を読み込むのを、初心者が初めて実装した場合、
158+
以下のようになるでしょう。</p>
152159
<pre><pre class="playground"><code class="language-rust norun edition2021"><span class="boring">#![allow(unused)]
153160
</span><span class="boring">fn main() {
154161
</span>use std::fs::read_to_string;
@@ -163,37 +170,63 @@ <h2 id="a-naive-approach"><a class="header" href="#a-naive-approach">A naive app
163170
result
164171
}
165172
<span class="boring">}</span></code></pre></pre>
166-
<p>Since the method <code>lines()</code> returns an iterator over the lines in the file,
173+
<!--
174+
Since the method `lines()` returns an iterator over the lines in the file,
167175
we can also perform a map inline and collect the results, yielding a more
168-
concise and fluent expression.</p>
176+
concise and fluent expression.
177+
-->
178+
<p><code>lines()</code>メソッドはファイルの各行のイテレータを返すので、
179+
インラインでマップを実行し結果を収集することもできます。
180+
そうすると、より簡潔で読みやすい表現となります。</p>
169181
<pre><pre class="playground"><code class="language-rust norun edition2021"><span class="boring">#![allow(unused)]
170182
</span><span class="boring">fn main() {
171183
</span>use std::fs::read_to_string;
172184

173185
fn read_lines(filename: &amp;str) -&gt; Vec&lt;String&gt; {
174186
read_to_string(filename)
175187
.unwrap() // panic on possible file-reading errors
188+
// ファイル読み込みエラーの場合はパニックする。
176189
.lines() // split the string into an iterator of string slices
190+
// 文字列のスライスのイテレータに分割する。
177191
.map(String::from) // make each slice into a string
192+
// スライスを文字列に変換する。
178193
.collect() // gather them together into a vector
194+
// ベクタにまとめる。
179195
}
180196
<span class="boring">}</span></code></pre></pre>
181-
<p>Note that in both examples above, we must convert the <code>&amp;str</code> reference
182-
returned from <code>lines()</code> to the owned type <code>String</code>, using <code>.to_string()</code>
183-
and <code>String::from</code> respectively.</p>
184-
<h2 id="a-more-efficient-approach"><a class="header" href="#a-more-efficient-approach">A more efficient approach</a></h2>
185-
<p>Here we pass ownership of the open <code>File</code> to a <code>BufReader</code> struct. <code>BufReader</code> uses an internal
186-
buffer to reduce intermediate allocations.</p>
187-
<p>We also update <code>read_lines</code> to return an iterator instead of allocating new
188-
<code>String</code> objects in memory for each line.</p>
197+
<!--
198+
Note that in both examples above, we must convert the `&str` reference
199+
returned from `lines()` to the owned type `String`, using `.to_string()`
200+
and `String::from` respectively.
201+
-->
202+
<p>上の例では、<code>lines()</code>から返された<code>&amp;str</code>
203+
それぞれ<code>to_string()</code><code>String::from</code>を使って、
204+
所有権のある<code>String</code>型に変換しなければならない点に注意してください。</p>
205+
<!--
206+
## A more efficient approach
207+
-->
208+
<h2 id="より効率的なやり方"><a class="header" href="#より効率的なやり方">より効率的なやり方</a></h2>
209+
<!--
210+
Here we pass ownership of the open `File` to a `BufReader` struct. `BufReader` uses an internal
211+
buffer to reduce intermediate allocations.
212+
-->
213+
<p>ここでは、開いた<code>File</code>の所有権を<code>BufReader</code>構造体に渡します。
214+
<code>BufReader</code>は内部的なバッファを使い、中間のメモリ割り当てを削減します。</p>
215+
<!--
216+
We also update `read_lines` to return an iterator instead of allocating new
217+
`String` objects in memory for each line.
218+
-->
219+
<p><code>read_lines</code>を更新して、それぞれの行に対してメモリ上に新しい<code>String</code>オブジェクトを割り当てるのではなく、イテレータを返すようにします。</p>
189220
<pre><pre class="playground"><code class="language-rust no_run edition2021">use std::fs::File;
190221
use std::io::{self, BufRead};
191222
use std::path::Path;
192223

193224
fn main() {
194225
// File hosts.txt must exist in the current path
226+
// hosts.txtファイルは現在のパスに存在しなければならない。
195227
if let Ok(lines) = read_lines(&quot;./hosts.txt&quot;) {
196228
// Consumes the iterator, returns an (Optional) String
229+
// イテレータを消費し、Option型のStringを返す。
197230
for line in lines {
198231
if let Ok(ip) = line {
199232
println!(&quot;{}&quot;, ip);
@@ -204,21 +237,34 @@ <h2 id="a-more-efficient-approach"><a class="header" href="#a-more-efficient-app
204237

205238
// The output is wrapped in a Result to allow matching on errors
206239
// Returns an Iterator to the Reader of the lines of the file.
240+
// 出力はResult型にラップされ、エラーをマッチできるようになる。
241+
// ファイルの各行のReaderへのイテレータを返す。
207242
fn read_lines&lt;P&gt;(filename: P) -&gt; io::Result&lt;io::Lines&lt;io::BufReader&lt;File&gt;&gt;&gt;
208243
where P: AsRef&lt;Path&gt;, {
209244
let file = File::open(filename)?;
210245
Ok(io::BufReader::new(file).lines())
211246
}</code></pre></pre>
212-
<p>Running this program simply prints the lines individually.</p>
247+
<!--
248+
Running this program simply prints the lines individually.
249+
-->
250+
<p>このプログラムを実行すると、単に各行をプリントします。</p>
213251
<pre><code class="language-shell">$ echo -e &quot;127.0.0.1\n192.168.0.1\n&quot; &gt; hosts.txt
214252
$ rustc read_lines.rs &amp;&amp; ./read_lines
215253
127.0.0.1
216254
192.168.0.1
217255
</code></pre>
218-
<p>(Note that since <code>File::open</code> expects a generic <code>AsRef&lt;Path&gt;</code> as argument, we define our
219-
generic <code>read_lines()</code> method with the same generic constraint, using the <code>where</code> keyword.)</p>
220-
<p>This process is more efficient than creating a <code>String</code> in memory with all of the file's
221-
contents. This can especially cause performance issues when working with larger files.</p>
256+
<!--
257+
(Note that since `File::open` expects a generic `AsRef<Path>` as argument, we define our
258+
generic `read_lines()` method with the same generic constraint, using the `where` keyword.)
259+
-->
260+
<p><code>File::open</code>はジェネリックな<code>AsRef&lt;Path&gt;</code>を引数にとるので、
261+
ジェネリックな<code>read_lines</code>メソッドも、<code>where</code>キーワードを使って、同じジェネリックな制約を持たせています。</p>
262+
<!--
263+
This process is more efficient than creating a `String` in memory with all of the file's
264+
contents. This can especially cause performance issues when working with larger files.
265+
-->
266+
<p>この処理は、ファイルの中身全てをメモリ上の<code>String</code>にするよりも効率的です。
267+
メモリ上に<code>String</code>を作ると、より大きなファイルを取り扱う際に、パフォーマンスの問題につながります。</p>
222268

223269
</main>
224270

0 commit comments

Comments
 (0)