@@ -2262,9 +2262,7 @@ <h3><a class="header" href="#乱数を生成する" id="乱数を生成する">
2262
2262
<span class="filename">Filename: src/main.rs</span>
2263
2263
-->
2264
2264
<p><span class="filename">ファイル名: src/main.rs</span></p>
2265
- <pre><code class="language-rust ignore">extern crate rand;
2266
-
2267
- use std::io;
2265
+ <pre><code class="language-rust ignore">use std::io;
2268
2266
use rand::Rng;
2269
2267
2270
2268
fn main() {
@@ -2278,7 +2276,8 @@ <h3><a class="header" href="#乱数を生成する" id="乱数を生成する">
2278
2276
2279
2277
let mut guess = String::new();
2280
2278
2281
- io::stdin().read_line(&mut guess)
2279
+ io::stdin()
2280
+ .read_line(&mut guess)
2282
2281
.expect("Failed to read line");
2283
2282
2284
2283
println!("You guessed: {}", guess);
@@ -2290,33 +2289,26 @@ <h3><a class="header" href="#乱数を生成する" id="乱数を生成する">
2290
2289
-->
2291
2290
<p><span class="caption">リスト2-3: 乱数を生成するコードの追加</span></p>
2292
2291
<!--
2293
- First, we add a line that lets Rust know we’ll be using the `rand` crate as an
2294
- external dependency. This also does the equivalent of calling `use rand`, so
2295
- now we can call anything in the `rand` crate by placing `rand::` before it.
2296
- -->
2297
- <p>まず、コンパイラに<code>rand</code>クレートを外部依存として使用することを知らせる行を追加しています。
2298
- これにより、<code>use rand</code>を呼ぶのと同じ効果が得られるので、<code>rand</code>クレートのものを<code>rand::</code>という接頭辞をつけて呼び出せるようになりました。</p>
2299
- <!--
2300
- Next, we add another `use` line: `use rand::Rng`. The `Rng` trait defines
2292
+ First, we add a `use` line: `use rand::Rng`. The `Rng` trait defines
2301
2293
methods that random number generators implement, and this trait must be in
2302
2294
scope for us to use those methods. Chapter 10 will cover traits in detail.
2303
2295
-->
2304
- <p>次に、別の <code>use</code>行を追加しています: <code>use rand::Rng</code>ですね。<code>Rng</code>トレイトは乱数生成器が実装するメソッドを定義していて、
2296
+ <p>まず、 <code>use</code>行を追加しています: <code>use rand::Rng</code>ですね。<code>Rng</code>トレイトは乱数生成器が実装するメソッドを定義していて、
2305
2297
このトレイトがスコープにないと、メソッドを使用できないのです。トレイトについて詳しくは、
2306
2298
第10章で解説します。</p>
2307
2299
<!--
2308
- Also , we’re adding two more lines in the middle. The `rand::thread_rng` function
2300
+ Next , we’re adding two lines in the middle. The `rand::thread_rng` function
2309
2301
will give us the particular random number generator that we’re going to use:
2310
2302
one that is local to the current thread of execution and seeded by the
2311
- operating system. Next, we call the `gen_range` method on the random number
2303
+ operating system. Then we call the `gen_range` method on the random number
2312
2304
generator. This method is defined by the `Rng` trait that we brought into
2313
2305
scope with the `use rand::Rng` statement. The `gen_range` method takes two
2314
2306
numbers as arguments and generates a random number between them. It’s inclusive
2315
2307
on the lower bound but exclusive on the upper bound, so we need to specify `1`
2316
2308
and `101` to request a number between 1 and 100.
2317
2309
-->
2318
- <p>また、途中に2行追加もしています 。<code>rand::thread_rng</code>関数は、これから使う特定の乱数生成器を返してくれます: この乱数生成器は、実行スレッドに固有で、OSにより、シード値を与えられています。
2319
- 次に 、この乱数生成器の<code>gen_range</code>メソッドを呼び出しています。このメソッドは、
2310
+ <p>次に、途中に2行を追加しています 。<code>rand::thread_rng</code>関数は、これから使う特定の乱数生成器を返してくれます: この乱数生成器は、実行スレッドに固有で、OSにより、シード値を与えられています。
2311
+ そして 、この乱数生成器の<code>gen_range</code>メソッドを呼び出しています。このメソッドは、
2320
2312
<code>use rand::Rng</code>文でスコープに導入した<code>Rng</code>トレイトで定義されています。<code>gen_range</code>メソッドは二つの数字を引数に取り、
2321
2313
それらの間の乱数を生成してくれます。範囲は下限値を含み、上限値を含まないため、<code>1</code>と<code>101</code>と指定しないと1から100の範囲の数字は得られません。</p>
2322
2314
<!--
@@ -2384,9 +2376,7 @@ <h2><a class="header" href="#予想と秘密の数字を比較する" id="予想
2384
2376
<span class="filename">Filename: src/main.rs</span>
2385
2377
-->
2386
2378
<p><span class="filename">ファイル名: src/main.rs</span></p>
2387
- <pre><code class="language-rust ignore">extern crate rand;
2388
-
2389
- use std::io;
2379
+ <pre><code class="language-rust ignore">use std::io;
2390
2380
use std::cmp::Ordering;
2391
2381
use rand::Rng;
2392
2382
@@ -2894,11 +2884,9 @@ <h3><a class="header" href="#不正な入力を処理する" id="不正な入力
2894
2884
<span class="filename">Filename: src/main.rs</span>
2895
2885
-->
2896
2886
<p><span class="filename">ファイル名: src/main.rs</span></p>
2897
- <pre><code class="language-rust ignore">extern crate rand;
2898
-
2899
- use std::io;
2887
+ <pre><code class="language-rust ignore">use rand::Rng;
2900
2888
use std::cmp::Ordering;
2901
- use rand::Rng ;
2889
+ use std::io ;
2902
2890
2903
2891
fn main() {
2904
2892
println!("Guess the number!");
@@ -2910,7 +2898,8 @@ <h3><a class="header" href="#不正な入力を処理する" id="不正な入力
2910
2898
2911
2899
let mut guess = String::new();
2912
2900
2913
- io::stdin().read_line(&mut guess)
2901
+ io::stdin()
2902
+ .read_line(&mut guess)
2914
2903
.expect("Failed to read line");
2915
2904
2916
2905
let guess: u32 = match guess.trim().parse() {
0 commit comments