@@ -973,8 +973,6 @@ Now that you've added the `rand` crate to *Cargo.toml*, let's start using
973
973
<span class =" filename " >ファイル名: src/main.rs</span >
974
974
975
975
``` rust,ignore
976
- extern crate rand;
977
-
978
976
use std::io;
979
977
use rand::Rng;
980
978
@@ -989,7 +987,8 @@ fn main() {
989
987
990
988
let mut guess = String::new();
991
989
992
- io::stdin().read_line(&mut guess)
990
+ io::stdin()
991
+ .read_line(&mut guess)
993
992
.expect("Failed to read line");
994
993
995
994
println!("You guessed: {}", guess);
@@ -1004,38 +1003,29 @@ random number</span>
1004
1003
<span class =" caption " >リスト2-3: 乱数を生成するコードの追加</span >
1005
1004
1006
1005
<!--
1007
- First, we add a line that lets Rust know we’ll be using the `rand` crate as an
1008
- external dependency. This also does the equivalent of calling `use rand`, so
1009
- now we can call anything in the `rand` crate by placing `rand::` before it.
1010
- -->
1011
-
1012
- まず、コンパイラに` rand ` クレートを外部依存として使用することを知らせる行を追加しています。
1013
- これにより、` use rand ` を呼ぶのと同じ効果が得られるので、` rand ` クレートのものを` rand:: ` という接頭辞をつけて呼び出せるようになりました。
1014
-
1015
- <!--
1016
- Next, we add another `use` line: `use rand::Rng`. The `Rng` trait defines
1006
+ First, we add a `use` line: `use rand::Rng`. The `Rng` trait defines
1017
1007
methods that random number generators implement, and this trait must be in
1018
1008
scope for us to use those methods. Chapter 10 will cover traits in detail.
1019
1009
-->
1020
1010
1021
- 次に、別の ` use ` 行を追加しています: ` use rand::Rng ` ですね。` Rng ` トレイトは乱数生成器が実装するメソッドを定義していて、
1011
+ まず、 ` use ` 行を追加しています: ` use rand::Rng ` ですね。` Rng ` トレイトは乱数生成器が実装するメソッドを定義していて、
1022
1012
このトレイトがスコープにないと、メソッドを使用できないのです。トレイトについて詳しくは、
1023
1013
第10章で解説します。
1024
1014
1025
1015
<!--
1026
- Also , we’re adding two more lines in the middle. The `rand::thread_rng` function
1016
+ Next , we’re adding two lines in the middle. The `rand::thread_rng` function
1027
1017
will give us the particular random number generator that we’re going to use:
1028
1018
one that is local to the current thread of execution and seeded by the
1029
- operating system. Next, we call the `gen_range` method on the random number
1019
+ operating system. Then we call the `gen_range` method on the random number
1030
1020
generator. This method is defined by the `Rng` trait that we brought into
1031
1021
scope with the `use rand::Rng` statement. The `gen_range` method takes two
1032
1022
numbers as arguments and generates a random number between them. It’s inclusive
1033
1023
on the lower bound but exclusive on the upper bound, so we need to specify `1`
1034
1024
and `101` to request a number between 1 and 100.
1035
1025
-->
1036
1026
1037
- また、途中に2行追加もしています 。` rand::thread_rng ` 関数は、これから使う特定の乱数生成器を返してくれます: この乱数生成器は、実行スレッドに固有で、OSにより、シード値を与えられています。
1038
- 次に 、この乱数生成器の` gen_range ` メソッドを呼び出しています。このメソッドは、
1027
+ 次に、途中に2行を追加しています 。` rand::thread_rng ` 関数は、これから使う特定の乱数生成器を返してくれます: この乱数生成器は、実行スレッドに固有で、OSにより、シード値を与えられています。
1028
+ そして 、この乱数生成器の` gen_range ` メソッドを呼び出しています。このメソッドは、
1039
1029
` use rand::Rng ` 文でスコープに導入した` Rng ` トレイトで定義されています。` gen_range ` メソッドは二つの数字を引数に取り、
1040
1030
それらの間の乱数を生成してくれます。範囲は下限値を含み、上限値を含まないため、` 1 ` と` 101 ` と指定しないと1から100の範囲の数字は得られません。
1041
1031
@@ -1119,8 +1109,6 @@ will explain.
1119
1109
<span class =" filename " >ファイル名: src/main.rs</span >
1120
1110
1121
1111
``` rust,ignore
1122
- extern crate rand;
1123
-
1124
1112
use std::io;
1125
1113
use std::cmp::Ordering;
1126
1114
use rand::Rng;
@@ -1730,11 +1718,9 @@ secret number. Listing 2-6 shows the final code:
1730
1718
<span class =" filename " >ファイル名: src/main.rs</span >
1731
1719
1732
1720
``` rust,ignore
1733
- extern crate rand;
1734
-
1735
- use std::io;
1736
- use std::cmp::Ordering;
1737
1721
use rand::Rng;
1722
+ use std::cmp::Ordering;
1723
+ use std::io;
1738
1724
1739
1725
fn main() {
1740
1726
println!("Guess the number!");
@@ -1746,7 +1732,8 @@ fn main() {
1746
1732
1747
1733
let mut guess = String::new();
1748
1734
1749
- io::stdin().read_line(&mut guess)
1735
+ io::stdin()
1736
+ .read_line(&mut guess)
1750
1737
.expect("Failed to read line");
1751
1738
1752
1739
let guess: u32 = match guess.trim().parse() {
0 commit comments