Skip to content

Commit

Permalink
ch05 構造体を使用して関係のあるデータを構造化するの和訳を最新版に更新
Browse files Browse the repository at this point in the history
  • Loading branch information
shinmili committed Feb 4, 2025
1 parent f5f69e9 commit 40d8fd5
Show file tree
Hide file tree
Showing 66 changed files with 973 additions and 774 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// ANCHOR: here
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
active: bool,
}
// ANCHOR_END: here

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
active: bool,
}

// ANCHOR: here
fn main() {
// ANCHOR: here
let user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
username: String::from("someusername123"),
email: String::from("[email protected]"),
sign_in_count: 1,
};
// ANCHOR_END: here
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
active: bool,
}

// ANCHOR: here
fn main() {
// ANCHOR: here
let mut user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
username: String::from("someusername123"),
email: String::from("[email protected]"),
sign_in_count: 1,
};

user1.email = String::from("[email protected]");
// ANCHOR_END: here
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
active: bool,
}

// ANCHOR: here
fn build_user(email: String, username: String) -> User {
User {
email: email,
username: username,
active: true,
username: username,
email: email,
sign_in_count: 1,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
active: bool,
}

// ANCHOR: here
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true,
username,
email,
sign_in_count: 1,
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
active: bool,
}

// ANCHOR: here
fn main() {
// --snip--
// ANCHOR_END: here

let user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};

// ANCHOR: here

let user2 = User {
email: String::from("[email protected]"),
username: String::from("anotherusername567"),
active: user1.active,
username: user1.username,
email: String::from("[email protected]"),
sign_in_count: user1.sign_in_count,
};
// ANCHOR_END: here
}
// ANCHOR_END: here
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
active: bool,
}

// ANCHOR: here
fn main() {
// --snip--
// ANCHOR_END: here

let user1 = User {
email: String::from("[email protected]"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};

// ANCHOR: here

let user2 = User {
email: String::from("[email protected]"),
username: String::from("anotherusername567"),
..user1
};
// ANCHOR_END: here
}
// ANCHOR_END: here

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
name = "rectangles"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
$ cargo run
Compiling structs v0.1.0 (file:///projects/structs)
Compiling rectangles v0.1.0 (file:///projects/rectangles)
Finished dev [unoptimized + debuginfo] target(s) in 0.42s
Running `target/debug/structs`
Running `target/debug/rectangles`
The area of the rectangle is 1500 square pixels.
(長方形の面積は、1500平方ピクセルです)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ fn main() {
let height1 = 50;

println!(
// 長方形の面積は、{}平方ピクセルです
"The area of the rectangle is {} square pixels.",
area(width1, height1)
);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
name = "rectangles"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
name = "rectangles"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
name = "rectangles"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
$ cargo run
Compiling structs v0.1.0 (file:///projects/structs)
Compiling rectangles v0.1.0 (file:///projects/rectangles)
error[E0277]: `Rectangle` doesn't implement `std::fmt::Display`
(エラー: `Rectangle`は`std::fmt::Display`を実装していません)
--> src/main.rs:12:29
|
12 | println!("rect1 is {}", rect1);
| ^^^^^ `Rectangle` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Rectangle`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: required by `std::fmt::Display::fmt`

error: aborting due to previous error
(ヘルプ: `std::fmt::Display`は`Rectangle`に対して実装されていません)
(注釈: フォーマット文字列では代わりに`{:?}`(またはpretty-printするためには{:#?})が使用できるかもしれません)
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0277`.
error: could not compile `structs`.

To learn more, run the command again with --verbose.
error: could not compile `rectangles` (bin "rectangles") due to 1 previous error
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ fn main() {
height: 50,
};

// rect1は{}です
println!("rect1 is {}", rect1);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
name = "rectangles"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$ cargo run
Compiling structs v0.1.0 (file:///projects/structs)
Compiling rectangles v0.1.0 (file:///projects/rectangles)
Finished dev [unoptimized + debuginfo] target(s) in 0.48s
Running `target/debug/structs`
Running `target/debug/rectangles`
rect1 is Rectangle { width: 30, height: 50 }

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
[package]
name = "structs"
name = "rectangles"
version = "0.1.0"
authors = ["Your Name <[email protected]>"]
edition = "2018"
edition = "2021"

[dependencies]
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ fn main() {
};

println!(
// 長方形の面積は{}平方ピクセルです。
"The area of the rectangle is {} square pixels.",
rect1.area()
);
Expand Down
Loading

0 comments on commit 40d8fd5

Please sign in to comment.