Skip to content

Commit

Permalink
ch19 高度な機能の和訳を最新版に更新
Browse files Browse the repository at this point in the history
  • Loading branch information
shinmili committed May 26, 2024
1 parent 3c459bb commit 262987a
Show file tree
Hide file tree
Showing 86 changed files with 957 additions and 1,265 deletions.
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-01/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-02/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-03/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-04/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-05/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
29 changes: 15 additions & 14 deletions listings/ch19-advanced-features/listing-19-05/output.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
$ cargo run
Compiling unsafe-example v0.1.0 (file:///projects/unsafe-example)
error[E0499]: cannot borrow `*slice` as mutable more than once at a time
--> src/main.rs:6:30
error[E0499]: cannot borrow `*values` as mutable more than once at a time
(エラー: 一度に2回以上、`*values`を可変で借用できません)
--> src/main.rs:6:31
|
1 | fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
| - let's call the lifetime of this reference `'1`
1 | fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
| - let's call the lifetime of this reference `'1`
| (この参照のライフタイムを`'1`とします)
...
6 | (&mut slice[..mid], &mut slice[mid..])
| -------------------------^^^^^--------
| | | |
| | | second mutable borrow occurs here
6 | (&mut values[..mid], &mut values[mid..])
| --------------------------^^^^^^--------
| | | |
| | | second mutable borrow occurs here
| | | (2回目の可変参照はここで発生します)
| | first mutable borrow occurs here
| returning this value requires that `*slice` is borrowed for `'1`

error: aborting due to previous error
| | (1回目の可変参照はここで発生します)
| returning this value requires that `*values` is borrowed for `'1`
| (この値を返すためには`*values`が`'1`の間借用されていることが必要です)

For more information about this error, try `rustc --explain E0499`.
error: could not compile `unsafe-example`.

To learn more, run the command again with --verbose.
error: could not compile `unsafe-example` (bin "unsafe-example") due to 1 previous error
6 changes: 3 additions & 3 deletions listings/ch19-advanced-features/listing-19-05/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ANCHOR: here
fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
let len = slice.len();
fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
let len = values.len();

assert!(mid <= len);

(&mut slice[..mid], &mut slice[mid..])
(&mut values[..mid], &mut values[mid..])
}
// ANCHOR_END: here

Expand Down
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-06/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
6 changes: 3 additions & 3 deletions listings/ch19-advanced-features/listing-19-06/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// ANCHOR: here
use std::slice;

fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
let len = slice.len();
let ptr = slice.as_mut_ptr();
fn split_at_mut(values: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
let len = values.len();
let ptr = values.as_mut_ptr();

assert!(mid <= len);

Expand Down
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-07/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
2 changes: 1 addition & 1 deletion listings/ch19-advanced-features/listing-19-07/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ fn main() {
let address = 0x01234usize;
let r = address as *mut i32;

let slice: &[i32] = unsafe { slice::from_raw_parts_mut(r, 10000) };
let values: &[i32] = unsafe { slice::from_raw_parts_mut(r, 10000) };
// ANCHOR_END: here
}
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-08/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
1 change: 1 addition & 0 deletions listings/ch19-advanced-features/listing-19-08/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ extern "C" {

fn main() {
unsafe {
// -3の絶対値は、Cによると{}
println!("Absolute value of -3 according to C: {}", abs(-3));
}
}
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-09/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-10/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-11/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "unsafe-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
2 changes: 2 additions & 0 deletions listings/ch19-advanced-features/listing-19-11/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
unsafe trait Foo {
// methods go here
// メソッドがここに来る
}

unsafe impl Foo for i32 {
// method implementations go here
// メソッドの実装がここに来る
}

fn main() {}
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-12/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-13/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-14/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
2 changes: 1 addition & 1 deletion listings/ch19-advanced-features/listing-19-14/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Add;

#[derive(Debug, PartialEq)]
#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
x: i32,
y: i32,
Expand Down
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-15/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-16/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 3 additions & 0 deletions listings/ch19-advanced-features/listing-19-16/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ struct Human;

impl Pilot for Human {
fn fly(&self) {
// こちら機長です。
println!("This is your captain speaking.");
}
}

impl Wizard for Human {
fn fly(&self) {
// 上がれ!
println!("Up!");
}
}

impl Human {
fn fly(&self) {
// *激しく腕を振る*
println!("*waving arms furiously*");
}
}
Expand Down
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-17/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-18/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-19/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 3 additions & 0 deletions listings/ch19-advanced-features/listing-19-19/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ struct Dog;

impl Dog {
fn baby_name() -> String {
// スポット
String::from("Spot")
}
}

impl Animal for Dog {
fn baby_name() -> String {
// 子犬
String::from("puppy")
}
}

fn main() {
// 赤ちゃん犬は{}と呼ばれる
println!("A baby dog is called a {}", Dog::baby_name());
}
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-20/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
23 changes: 13 additions & 10 deletions listings/ch19-advanced-features/listing-19-20/output.txt
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
$ cargo run
Compiling traits-example v0.1.0 (file:///projects/traits-example)
error[E0283]: type annotations needed
error[E0790]: cannot call associated function on trait without specifying the corresponding `impl` type
(エラー: 対応する`impl`型を指定せずにトレイト上の関連関数を呼び出すことはできません)
--> src/main.rs:20:43
|
2 | fn baby_name() -> String;
| ------------------------- required by `Animal::baby_name`
| ------------------------- `Animal::baby_name` defined here
| (`Animal::baby_name`はここで定義されています)
...
20 | println!("A baby dog is called a {}", Animal::baby_name());
| ^^^^^^^^^^^^^^^^^ cannot infer type
| ^^^^^^^^^^^^^^^^^ cannot call associated function of trait
| (トレイトの関連関数を呼び出すことができません)
|
= note: cannot resolve `_: Animal`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0283`.
error: could not compile `traits-example`.
help: use the fully-qualified path to the only available implementation
(ヘルプ: 利用可能な実装のうち1つを指すフルパスを使用してください)
|
20 | println!("A baby dog is called a {}", <Dog as Animal>::baby_name());
| +++++++ +

To learn more, run the command again with --verbose.
For more information about this error, try `rustc --explain E0790`.
error: could not compile `traits-example` (bin "traits-example") due to 1 previous error
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-21/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-22/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-23/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "traits-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-24/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "types-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
3 changes: 1 addition & 2 deletions listings/ch19-advanced-features/listing-19-25/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "types-example"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Loading

0 comments on commit 262987a

Please sign in to comment.