@@ -182,16 +182,18 @@ <h2 id="データ型"><a class="header" href="#データ型">データ型</a></h
182
182
-->
183
183
< p > ここで型注釈を付けなければ、コンパイラは以下のエラーを表示し、これは可能性のある型のうち、
184
184
どの型を使用したいのかを知るのに、コンパイラがプログラマからもっと情報を得る必要があることを意味します:</ p >
185
- < pre > < code class ="language-text "> error[E0282]: type annotations needed
185
+ < pre > < code class ="language-console "> $ cargo build
186
+ Compiling no_type_annotations v0.1.0 (file:///projects/no_type_annotations)
187
+ error[E0282]: type annotations needed
186
188
(型注釈が必要です)
187
189
--> src/main.rs:2:9
188
190
|
189
191
2 | let guess = "42".parse().expect("Not a number!");
190
- | ^^^^^ cannot infer type for `_`
191
- | (`_`の型が推論できません )
192
- |
193
- = note: type annotations or generic parameter binding required
194
- (注釈: 型注釈、またはジェネリクス引数束縛が必要です)
192
+ | ^^^^^ consider giving `guess` a type
193
+ | (`guess`に型を与えることを検討してください )
194
+
195
+ For more information about this error, try `rustc --explain E0282`.
196
+ error: could not compile `no_type_annotations` due to previous error
195
197
</ code > </ pre >
196
198
<!--
197
199
You’ll see different type annotations for other data types.
@@ -369,39 +371,26 @@ <h4 id="数値演算"><a class="header" href="#数値演算">数値演算</a></h
369
371
<span class="filename">Filename: src/main.rs</span>
370
372
-->
371
373
< p > < span class ="filename "> ファイル名: src/main.rs</ span > </ p >
372
- <!--
373
- ```rust
374
- fn main() {
375
- // addition
376
- let sum = 5 + 10;
377
-
378
- // subtraction
379
- let difference = 95.5 - 4.3;
380
-
381
- // multiplication
382
- let product = 4 * 30;
383
-
384
- // division
385
- let quotient = 56.7 / 32.2;
386
-
387
- // remainder
388
- let remainder = 43 % 5;
389
- }
390
- ```
391
- -->
392
374
< pre > < pre class ="playground "> < code class ="language-rust "> fn main() {
375
+ // addition
393
376
// 足し算
394
377
let sum = 5 + 10;
395
378
379
+ // subtraction
396
380
// 引き算
397
381
let difference = 95.5 - 4.3;
398
382
383
+ // multiplication
399
384
// 掛け算
400
385
let product = 4 * 30;
401
386
387
+ // division
402
388
// 割り算
403
389
let quotient = 56.7 / 32.2;
390
+ let floored = 2 / 3; // Results in 0
391
+ // 結果は0
404
392
393
+ // remainder
405
394
// 余り
406
395
let remainder = 43 % 5;
407
396
}
@@ -429,18 +418,11 @@ <h4 id="論理値型"><a class="header" href="#論理値型">論理値型</a></h
429
418
<span class="filename">Filename: src/main.rs</span>
430
419
-->
431
420
< p > < span class ="filename "> ファイル名: src/main.rs</ span > </ p >
432
- <!--
433
- ```rust
434
- fn main() {
435
- let t = true;
436
- let f: bool = false; // with explicit type annotation
437
- }
438
- ```
439
- -->
440
421
< pre > < pre class ="playground "> < code class ="language-rust "> fn main() {
441
422
let t = true;
442
423
443
- let f: bool = false; // 明示的型注釈付きで
424
+ let f: bool = false; // with explicit type annotation
425
+ // 明示的型注釈付きで
444
426
}
445
427
</ code > </ pre > </ pre >
446
428
<!--
@@ -637,6 +619,40 @@ <h4 id="配列型"><a class="header" href="#配列型">配列型</a></h4>
637
619
< span class ="boring "> }
638
620
</ span > </ code > </ pre > </ pre >
639
621
<!--
622
+ You write an array’s type using square brackets with the type of each element,
623
+ a semicolon, and then the number of elements in the array, like so:
624
+ -->
625
+ < p > 例えば次のように、配列の型は角かっこの中に要素の型とセミコロン、そして配列の要素数を与えます。</ p >
626
+ < pre > < pre class ="playground "> < code class ="language-rust ">
627
+ < span class ="boring "> #![allow(unused)]
628
+ </ span > < span class ="boring "> fn main() {
629
+ </ span > let a: [i32; 5] = [1, 2, 3, 4, 5];
630
+ < span class ="boring "> }
631
+ </ span > </ code > </ pre > </ pre >
632
+ <!--
633
+ Here, `i32` is the type of each element. After the semicolon, the number `5`
634
+ indicates the array contains five elements.
635
+ -->
636
+ < p > ここでの< code > i32</ code > は要素の型です。セミコロンのあとの< code > 5</ code > という数字は配列の要素が5つあることを表しています。</ p >
637
+ <!--
638
+ You can also initialize an array to contain the same value for each element by
639
+ specifying the initial value, followed by a semicolon, and then the length of
640
+ the array in square brackets, as shown here:
641
+ -->
642
+ < p > 次のように、角かっこの中に初期値とセミコロン、そして配列の長さを与えることで、各要素に同じ値を持つように配列を初期化することができます。</ p >
643
+ < pre > < pre class ="playground "> < code class ="language-rust ">
644
+ < span class ="boring "> #![allow(unused)]
645
+ </ span > < span class ="boring "> fn main() {
646
+ </ span > let a = [3; 5];
647
+ < span class ="boring "> }
648
+ </ span > </ code > </ pre > </ pre >
649
+ <!--
650
+ The array named `a` will contain `5` elements that will all be set to the value
651
+ `3` initially. This is the same as writing `let a = [3, 3, 3, 3, 3];` but in a
652
+ more concise way.
653
+ -->
654
+ < p > この< code > a</ code > という名前の配列は< code > 3</ code > という値が5つあるものです。これは< code > let a = [3, 3, 3, 3, 3];</ code > と書くのと同じですが、より簡潔になります。</ p >
655
+ <!--
640
656
##### Accessing Array Elements
641
657
-->
642
658
< h5 id ="配列の要素にアクセスする "> < a class ="header " href ="#配列の要素にアクセスする "> 配列の要素にアクセスする</ a > </ h5 >
@@ -679,28 +695,52 @@ <h5 id="配列要素への無効なアクセス"><a class="header" href="#配列
679
695
<span class="filename">Filename: src/main.rs</span>
680
696
-->
681
697
< p > < span class ="filename "> ファイル名: src/main.rs</ span > </ p >
682
- < pre > < code class ="language-rust ignore "> fn main() {
698
+ < pre > < code class ="language-rust ignore panics "> use std::io;
699
+
700
+ fn main() {
683
701
let a = [1, 2, 3, 4, 5];
684
- let index = 10;
702
+
703
+ println!("Please enter an array index.");
704
+ // 配列の何番目の要素にアクセスするか指定してください
705
+
706
+ let mut index = String::new();
707
+
708
+ io::stdin()
709
+ .read_line(&mut index)
710
+ .expect("Failed to read line");
711
+ // 値の読み込みに失敗しました
712
+
713
+ let index: usize = index
714
+ .trim()
715
+ .parse()
716
+ .expect("Index entered was not a number");
717
+ // 入力された値は数字ではありません
685
718
686
719
let element = a[index];
687
720
688
- println!("The value of element is: {}", element); // 要素の値は{}です
721
+ println!(
722
+ "The value of the element at index {} is: {}",
723
+ // {}番目の要素の値は{}です
724
+ index, element
725
+ );
689
726
}
690
727
</ code > </ pre >
691
728
<!--
692
- Running this code using `cargo run` produces the following result:
729
+ This code compiles successfully. If you run this code using `cargo run` and
730
+ enter 0, 1, 2, 3, or 4, the program will print out the corresponding value at
731
+ that index in the array. If you instead enter a number past the end of the
732
+ array, such as 10, you’ll see output like this:
733
+ -->
734
+ < p > このコードはコンパイルされます。< code > cargo run</ code > で走らせ、0, 1, 2, 3, または4をこのプログラムに入力すると配列の対応する値を出力します。もし配列の末尾を超えるような、例えば10などの数字を与えると、次のような出力が表示されます。</ p >
735
+ <!-- manual-regeneration
736
+ cd listings/ch03-common-programming-concepts/no-listing-15-invalid-array-access
737
+ cargo run
738
+ 10
693
739
-->
694
- < p > このコードを< code > cargo run</ code > で走らせると、以下のような結果になります:</ p >
695
- < pre > < code class ="language-text "> $ cargo run
696
- Compiling arrays v0.1.0 (file:///projects/arrays)
697
- Finished dev [unoptimized + debuginfo] target(s) in 0.31 secs
698
- Running `target/debug/arrays`
699
- thread '<main>' panicked at 'index out of bounds: the len is 5 but the index is
700
- 10', src/main.rs:6
701
- スレッド'<main>'は'範囲外アクセス: 長さは5ですが、添え字は10でした', src/main.rs:6
740
+ < pre > < code class ="language-console "> thread 'main' panicked at 'index out of bounds: the len is 5 but the index is 10', src/main.rs:19:19
741
+ スレッド'main'は'範囲外アクセス: 長さは5ですが、添え字は10でした', src/main.rs:19:19
702
742
でパニックしました
703
- note: Run with `RUST_BACKTRACE=1` for a backtrace.
743
+ note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
704
744
</ code > </ pre >
705
745
<!--
706
746
The compilation didn’t produce any errors, but the program results in a
0 commit comments