回答編集履歴

5

誤字を修正しました

2021/04/06 12:52

投稿

tatsuya6502
tatsuya6502

スコア2035

test CHANGED
@@ -158,7 +158,7 @@
158
158
 
159
159
  ```console
160
160
 
161
- % cargo run
161
+ $ cargo run
162
162
 
163
163
  Finished dev [unoptimized + debuginfo] target(s) in 0.46s
164
164
 

4

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

2021/04/06 12:52

投稿

tatsuya6502
tatsuya6502

スコア2035

test CHANGED
@@ -174,9 +174,11 @@
174
174
 
175
175
 
176
176
 
177
- ```
177
+ ```console
178
-
178
+
179
- # 中国に住んでいるので、日本との時差が1時間あります。(中国 19:53 → 日本 20:53)
179
+ ## 私は中国に住んでいるので、日本との時差が1時間あります。
180
+
181
+ ##(中国 19:53 → 日本 20:53)
180
182
 
181
183
 
182
184
 

3

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

2021/04/06 12:11

投稿

tatsuya6502
tatsuya6502

スコア2035

test CHANGED
@@ -91,3 +91,237 @@
91
91
  }
92
92
 
93
93
  ```
94
+
95
+
96
+
97
+ **追記**
98
+
99
+
100
+
101
+ > 回答いただいた内容に納得でき、修正したのですが同じエラーが発生してしまいます。
102
+
103
+ > 他に原因が思い当たる場所はございますでしょうか?
104
+
105
+
106
+
107
+ うーん。おかしいですですね。なぜでしょう。
108
+
109
+
110
+
111
+ もしかして`cargo run`ではなくて、`rustc`を直接実行してますか? その場合は確かにエラーになるはずです。
112
+
113
+
114
+
115
+ ```console
116
+
117
+ $ rustc src/main.rs
118
+
119
+ error[E0432]: unresolved import `chapter10`
120
+
121
+ --> src/main.rs:1:17
122
+
123
+ |
124
+
125
+ 1 | use chapter10::{self, Summary, Tweet};
126
+
127
+ | ^^^^ no `chapter10` in the root
128
+
129
+
130
+
131
+ error[E0422]: cannot find struct, variant or union type `Tweet` in this scope
132
+
133
+ --> src/main.rs:4:17
134
+
135
+ |
136
+
137
+ 4 | let tweet = Tweet {
138
+
139
+ | ^^^^^ not found in this scope
140
+
141
+
142
+
143
+ error: aborting due to 2 previous errors
144
+
145
+
146
+
147
+ Some errors have detailed explanations: E0422, E0432.
148
+
149
+ For more information about an error, try `rustc --explain E0422`.
150
+
151
+ ```
152
+
153
+
154
+
155
+ もしそうでしたら`cargo run`で実行してください。
156
+
157
+
158
+
159
+ ```console
160
+
161
+ % cargo run
162
+
163
+ Finished dev [unoptimized + debuginfo] target(s) in 0.46s
164
+
165
+ Running `target/debug/chapter10`
166
+
167
+ 1 new tweet: horse_ebooks: of course, as you probably already know, people
168
+
169
+ ```
170
+
171
+
172
+
173
+ 私の方でも質問者さまと同じ構成で試しています。以下の内容で動きました。
174
+
175
+
176
+
177
+ ```
178
+
179
+ # 中国に住んでいるので、日本との時差が1時間あります。(中国 19:53 → 日本 20:53)
180
+
181
+
182
+
183
+ $ exa -lT ../trait_test
184
+
185
+ drwxr-xr-x - tatsuya 6 Apr 19:53 ../trait_test
186
+
187
+ .rw-r--r-- 93 tatsuya 6 Apr 19:48 ├── Cargo.toml
188
+
189
+ drwxr-xr-x - tatsuya 6 Apr 19:48 └── src
190
+
191
+ .rw-r--r-- 582 tatsuya 6 Apr 19:48 ├── lib.rs
192
+
193
+ .rw-r--r-- 419 tatsuya 6 Apr 19:48 └── main.rs
194
+
195
+
196
+
197
+ $ cat Cargo.toml
198
+
199
+ [package]
200
+
201
+ name = "chapter10"
202
+
203
+ version = "0.1.0"
204
+
205
+ authors = []
206
+
207
+ edition = "2018"
208
+
209
+
210
+
211
+ [dependencies]
212
+
213
+
214
+
215
+ $ cat src/main.rs
216
+
217
+ use chapter10::{self, Summary, Tweet};
218
+
219
+
220
+
221
+ fn main() {
222
+
223
+ let tweet = Tweet {
224
+
225
+ username: String::from("horse_ebooks"),
226
+
227
+ content: String::from(
228
+
229
+ // もちろん、ご存知かもしれませんがね、みなさん
230
+
231
+ "of course, as you probably already know, people",
232
+
233
+ ),
234
+
235
+ reply: false,
236
+
237
+ retweet: false,
238
+
239
+ };
240
+
241
+
242
+
243
+ println!("1 new tweet: {}", tweet.summarize());
244
+
245
+ }
246
+
247
+
248
+
249
+ $ cat src/lib.rs
250
+
251
+ pub trait Summary {
252
+
253
+ fn summarize(&self) -> String;
254
+
255
+ }
256
+
257
+
258
+
259
+ pub struct NewsArticle {
260
+
261
+ pub headline: String,
262
+
263
+ pub location: String,
264
+
265
+ pub author: String,
266
+
267
+ pub content: String,
268
+
269
+ }
270
+
271
+
272
+
273
+ impl Summary for NewsArticle {
274
+
275
+ fn summarize(&self) -> String {
276
+
277
+ format! {"{}, by {} ({})", self.headline, self.author, self.location}
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+ pub struct Tweet {
286
+
287
+ pub username: String,
288
+
289
+ pub content: String,
290
+
291
+ pub reply: bool,
292
+
293
+ pub retweet: bool,
294
+
295
+ }
296
+
297
+
298
+
299
+ impl Summary for Tweet {
300
+
301
+ fn summarize(&self) -> String {
302
+
303
+ format!("{}: {}", self.username, self.content)
304
+
305
+ }
306
+
307
+ }
308
+
309
+
310
+
311
+ $ rustc -V
312
+
313
+ rustc 1.51.0 (2fd73fabe 2021-03-23)
314
+
315
+
316
+
317
+ $ cargo run
318
+
319
+ Compiling chapter10 v0.1.0 (... /trait_test)
320
+
321
+ Finished dev [unoptimized + debuginfo] target(s) in 1.15s
322
+
323
+ Running `target/debug/chapter10`
324
+
325
+ 1 new tweet: horse_ebooks: of course, as you probably already know, people
326
+
327
+ ```

2

誤字を修正しました

2021/04/06 12:09

投稿

tatsuya6502
tatsuya6502

スコア2035

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  - `lib.rs`からアクセスする場合:`use crate;`
6
6
 
7
- - `main.rs`からアクセスする場合:`use クレート名`
7
+ - `main.rs`からアクセスする場合:`use クレート名;`
8
8
 
9
9
 
10
10
 

1

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

2021/04/04 05:03

投稿

tatsuya6502
tatsuya6502

スコア2035

test CHANGED
@@ -16,7 +16,7 @@
16
16
 
17
17
  [package]
18
18
 
19
- name = "chapter10" # この名前を使う。(ただし"-"があったら、"_"で置き換える)
19
+ name = "chapter10" # この名前を使う。(ただし"-"があったら、useでは"_"で置き換える)
20
20
 
21
21
  ```
22
22