質問編集履歴

3

誤植の訂正

2019/09/29 13:19

投稿

Yhaya
Yhaya

スコア439

test CHANGED
File without changes
test CHANGED
@@ -106,7 +106,7 @@
106
106
 
107
107
 
108
108
 
109
- return vec![num1, num2];
109
+ return vec![ans1, ans2];
110
110
 
111
111
  ```
112
112
 

2

試したことを追記

2019/09/29 13:18

投稿

Yhaya
Yhaya

スコア439

test CHANGED
File without changes
test CHANGED
@@ -91,3 +91,67 @@
91
91
  For more information about this error, try `rustc --explain E0308`.
92
92
 
93
93
  ```
94
+
95
+
96
+
97
+ 試したこと: as で usize -> i32 へ変換する
98
+
99
+
100
+
101
+ ```rust
102
+
103
+ let ans1 = i as i32;
104
+
105
+ let ans2 = j as i32;
106
+
107
+
108
+
109
+ return vec![num1, num2];
110
+
111
+ ```
112
+
113
+ をif文の中に入れたところ次のようなエラーが出ました。
114
+
115
+
116
+
117
+ ```bash
118
+
119
+ error[E0308]: mismatched types
120
+
121
+ --> src/main.rs:2:5
122
+
123
+ |
124
+
125
+ 1 | fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
126
+
127
+ | -------- expected `std::vec::Vec<i32>` because of return type
128
+
129
+ 2 | / for i in 0..(nums.len() - 1) {
130
+
131
+ 3 | | for j in i..nums.len() {
132
+
133
+ 4 | | if nums[i] + nums[j] == target {
134
+
135
+ 5 | | let ans1 = i as i32;
136
+
137
+ ... |
138
+
139
+ 10 | |
140
+
141
+ 11 | | }
142
+
143
+ | |_____^ expected struct `std::vec::Vec`, found ()
144
+
145
+ |
146
+
147
+ = note: expected type `std::vec::Vec<i32>`
148
+
149
+ found type `()`
150
+
151
+
152
+
153
+ error: aborting due to previous error
154
+
155
+ For more information about this error, try `rustc --explain E0308`.
156
+
157
+ ```

1

エラーメッセージの追記

2019/09/29 13:18

投稿

Yhaya
Yhaya

スコア439

test CHANGED
File without changes
test CHANGED
@@ -31,3 +31,63 @@
31
31
 
32
32
 
33
33
  Rust 自体が初心者なのでこの考え方自体があっているかどうかも知りたいです。よろしくお願いします。
34
+
35
+
36
+
37
+ (追記) エラーメッセージ
38
+
39
+ ```bash
40
+
41
+ error[E0308]: mismatched types
42
+
43
+ --> src/main.rs:5:29
44
+
45
+ |
46
+
47
+ 5 | return vec![i, j];
48
+
49
+ | ^ expected i32, found usize
50
+
51
+
52
+
53
+ error[E0308]: mismatched types
54
+
55
+ --> src/main.rs:2:5
56
+
57
+ |
58
+
59
+ 1 | fn my_func(nums: Vec<i32>, target: i32) -> Vec<i32> {
60
+
61
+ | -------- expected `std::vec::Vec<i32>` because of return type
62
+
63
+ 2 | / for i in 0..(nums.len() - 1) {
64
+
65
+ 3 | | for j in i..nums.len() {
66
+
67
+ 4 | | if nums[i] + nums[j] == target {
68
+
69
+ 5 | | return vec![i, j];
70
+
71
+ ... |
72
+
73
+ 8 | |
74
+
75
+ 9 | | }
76
+
77
+ | |_____^ expected struct `std::vec::Vec`, found ()
78
+
79
+ |
80
+
81
+ = note: expected type `std::vec::Vec<i32>`
82
+
83
+ found type `()`
84
+
85
+
86
+
87
+ error: aborting due to 2 previous errors
88
+
89
+
90
+
91
+ For more information about this error, try `rustc --explain E0308`.
92
+
93
+ ```