teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

5

誤字を修正しました

2021/04/06 12:52

投稿

tatsuya6502
tatsuya6502

スコア2055

answer CHANGED
@@ -78,7 +78,7 @@
78
78
  もしそうでしたら`cargo run`で実行してください。
79
79
 
80
80
  ```console
81
- % cargo run
81
+ $ cargo run
82
82
  Finished dev [unoptimized + debuginfo] target(s) in 0.46s
83
83
  Running `target/debug/chapter10`
84
84
  1 new tweet: horse_ebooks: of course, as you probably already know, people

4

コードブロックのフォーマットを修正しました

2021/04/06 12:52

投稿

tatsuya6502
tatsuya6502

スコア2055

answer CHANGED
@@ -86,8 +86,9 @@
86
86
 
87
87
  私の方でも質問者さまと同じ構成で試しています。以下の内容で動きました。
88
88
 
89
- ```
89
+ ```console
90
- # 中国に住んでいるので、日本との時差が1時間あります。(中国 19:53 → 日本 20:53)
90
+ ## 私は中国に住んでいるので、日本との時差が1時間あります。
91
+ ##(中国 19:53 → 日本 20:53)
91
92
 
92
93
  $ exa -lT ../trait_test
93
94
  drwxr-xr-x - tatsuya 6 Apr 19:53 ../trait_test

3

cargo runコマンドについて追記しました

2021/04/06 12:11

投稿

tatsuya6502
tatsuya6502

スコア2055

answer CHANGED
@@ -44,4 +44,121 @@
44
44
  format!("{}: {}", self.username, serlf.content)
45
45
  }
46
46
  }
47
+ ```
48
+
49
+ **追記**
50
+
51
+ > 回答いただいた内容に納得でき、修正したのですが同じエラーが発生してしまいます。
52
+ > 他に原因が思い当たる場所はございますでしょうか?
53
+
54
+ うーん。おかしいですですね。なぜでしょう。
55
+
56
+ もしかして`cargo run`ではなくて、`rustc`を直接実行してますか? その場合は確かにエラーになるはずです。
57
+
58
+ ```console
59
+ $ rustc src/main.rs
60
+ error[E0432]: unresolved import `chapter10`
61
+ --> src/main.rs:1:17
62
+ |
63
+ 1 | use chapter10::{self, Summary, Tweet};
64
+ | ^^^^ no `chapter10` in the root
65
+
66
+ error[E0422]: cannot find struct, variant or union type `Tweet` in this scope
67
+ --> src/main.rs:4:17
68
+ |
69
+ 4 | let tweet = Tweet {
70
+ | ^^^^^ not found in this scope
71
+
72
+ error: aborting due to 2 previous errors
73
+
74
+ Some errors have detailed explanations: E0422, E0432.
75
+ For more information about an error, try `rustc --explain E0422`.
76
+ ```
77
+
78
+ もしそうでしたら`cargo run`で実行してください。
79
+
80
+ ```console
81
+ % cargo run
82
+ Finished dev [unoptimized + debuginfo] target(s) in 0.46s
83
+ Running `target/debug/chapter10`
84
+ 1 new tweet: horse_ebooks: of course, as you probably already know, people
85
+ ```
86
+
87
+ 私の方でも質問者さまと同じ構成で試しています。以下の内容で動きました。
88
+
89
+ ```
90
+ # 中国に住んでいるので、日本との時差が1時間あります。(中国 19:53 → 日本 20:53)
91
+
92
+ $ exa -lT ../trait_test
93
+ drwxr-xr-x - tatsuya 6 Apr 19:53 ../trait_test
94
+ .rw-r--r-- 93 tatsuya 6 Apr 19:48 ├── Cargo.toml
95
+ drwxr-xr-x - tatsuya 6 Apr 19:48 └── src
96
+ .rw-r--r-- 582 tatsuya 6 Apr 19:48 ├── lib.rs
97
+ .rw-r--r-- 419 tatsuya 6 Apr 19:48 └── main.rs
98
+
99
+ $ cat Cargo.toml
100
+ [package]
101
+ name = "chapter10"
102
+ version = "0.1.0"
103
+ authors = []
104
+ edition = "2018"
105
+
106
+ [dependencies]
107
+
108
+ $ cat src/main.rs
109
+ use chapter10::{self, Summary, Tweet};
110
+
111
+ fn main() {
112
+ let tweet = Tweet {
113
+ username: String::from("horse_ebooks"),
114
+ content: String::from(
115
+ // もちろん、ご存知かもしれませんがね、みなさん
116
+ "of course, as you probably already know, people",
117
+ ),
118
+ reply: false,
119
+ retweet: false,
120
+ };
121
+
122
+ println!("1 new tweet: {}", tweet.summarize());
123
+ }
124
+
125
+ $ cat src/lib.rs
126
+ pub trait Summary {
127
+ fn summarize(&self) -> String;
128
+ }
129
+
130
+ pub struct NewsArticle {
131
+ pub headline: String,
132
+ pub location: String,
133
+ pub author: String,
134
+ pub content: String,
135
+ }
136
+
137
+ impl Summary for NewsArticle {
138
+ fn summarize(&self) -> String {
139
+ format! {"{}, by {} ({})", self.headline, self.author, self.location}
140
+ }
141
+ }
142
+
143
+ pub struct Tweet {
144
+ pub username: String,
145
+ pub content: String,
146
+ pub reply: bool,
147
+ pub retweet: bool,
148
+ }
149
+
150
+ impl Summary for Tweet {
151
+ fn summarize(&self) -> String {
152
+ format!("{}: {}", self.username, self.content)
153
+ }
154
+ }
155
+
156
+ $ rustc -V
157
+ rustc 1.51.0 (2fd73fabe 2021-03-23)
158
+
159
+ $ cargo run
160
+ Compiling chapter10 v0.1.0 (... /trait_test)
161
+ Finished dev [unoptimized + debuginfo] target(s) in 1.15s
162
+ Running `target/debug/chapter10`
163
+ 1 new tweet: horse_ebooks: of course, as you probably already know, people
47
164
  ```

2

誤字を修正しました

2021/04/06 12:09

投稿

tatsuya6502
tatsuya6502

スコア2055

answer CHANGED
@@ -1,7 +1,7 @@
1
1
  クレート(パッケージ)の`src`ディレクトリー内に`lib.rs`と`main.rs`がある場合、自分自身(自クレート)には以下のようにしてアクセスします。
2
2
 
3
3
  - `lib.rs`からアクセスする場合:`use crate;`
4
- - `main.rs`からアクセスする場合:`use クレート名`
4
+ - `main.rs`からアクセスする場合:`use クレート名;`
5
5
 
6
6
  ここで、クレート名は、Cargo.tomlの`[package]`→`name`で指定したものを使用します。
7
7
 

1

分かりやすさのために語を追加しました

2021/04/04 05:03

投稿

tatsuya6502
tatsuya6502

スコア2055

answer CHANGED
@@ -7,7 +7,7 @@
7
7
 
8
8
  ```toml
9
9
  [package]
10
- name = "chapter10" # この名前を使う。(ただし"-"があったら、"_"で置き換える)
10
+ name = "chapter10" # この名前を使う。(ただし"-"があったら、useでは"_"で置き換える)
11
11
  ```
12
12
 
13
13
  ですから、ディレクトリー構成は「ケース1」で、Cargo.tomlの`[package]`→`name`が`"chapter10"`になっているか確認してください。(ディレクトリー名は`chapter10`でなくても大丈夫です)