error[E0599]: no method named `method` found for struct `MyType` in the current scope
error[E0599]: the method `method` exists for struct `MyType<Xxx>`, but its trait bounds were not satisfied
無効なメソッドの呼出を行った。
呼び出そうとしたメソッドが型にそもそもないパターン。
以下では、型 MyValue にメソッド method が存在しない。
fn main() {
let value = MyValue();
value.method();
}
struct MyValue();
error[E0599]: no method named `method` found for struct `MyValue` in the current scope --> src\main.rs:3:11 | 3 | value.method(); | ^^^^^^ method not found in `MyValue` ... 6 | struct MyValue(); | -------------- method `method` not found for this struct
呼び出そうとしたメソッドは型にあるが、境界に適合しないパターン。
以下では、型 MyValue<T> にメソッド print_binary があるが、境界に適合せず呼べない。なぜなら、型パラメタ T に割り当てられた型は bool であり、これは T が UpperHex を実装すべきとした実装時の境界に違反する。
use std::fmt::UpperHex;
fn main() {
let value = MyValue(true);
value.print_binary();
}
struct MyValue<T>(T);
impl<T: UpperHex> MyValue<T> {
fn print_binary(&self) {
println!("{:X}", self.0)
}
}
error[E0599]: the method `print_binary` exists for struct `MyValue<bool>`, but its trait bounds were not satisfied
--> src\main.rs:5:11
|
5 | value.print_binary();
| ^^^^^^^^^^^^ method cannot be called on `MyValue<bool>` due to unsatisfied trait bounds
...
8 | struct MyValue<T>(T);
| ----------------- method `print_binary` not found for this struct
|
note: trait bound `bool: UpperHex` was not satisfied
--> src\main.rs:9:9
|
9 | impl<T: UpperHex> MyValue<T> {
| ^^^^^^^^ ----------
| |
| unsatisfied trait bound introduced here
derive 属性版)
呼び出そうとしたメソッドは型にあるが、そのメソッドが derive 属性により導入されたトレイトのメソッドで、その境界に適合しないパターン。
『derive 属性 - 型パラメタの影響』で紹介する通り、derive 属性は型パラメタについての境界を導入する場合が多い。この境界に違反するとエラーになる。なお、下記のサンプルではエラーメッセージがその境界 T: Default について指摘しているが、ソース中に該当箇所が存在しない。そのため、混乱しやすい。
以下では、MyVec<T> の Deault の実装は derive 属性に由来するため、T: Default の境界を持つ。しかし、MyVec::<T>::default() における T は Clone しか実装していない。
fn main() {
let values = values(1, 3);
assert_eq!(values.0, vec![1, 1, 1]);
}
fn values<T: Clone>(value: T, len: usize) -> MyVec<T> {
let mut ret = MyVec::<T>::default();
for _ in 0..len {
ret.0.push(value.clone());
}
ret
}
#[derive(Default, Clone, PartialEq)]
struct MyVec<T>(Vec<T>);
error[E0599]: the function or associated item `default` exists for struct `MyVec<T>`, but its trait bounds were not satisfied
--> src\main.rs:7:31
|
7 | let mut ret = MyVec::<T>::default();
| ^^^^^^^ function or associated item cannot be called on `MyVec<T>` due to unsatisfied trait bounds
...
16 | struct MyVec<T>(Vec<T>);
| --------------- function or associated item `default` not found for this struct because it doesn't satisfy `MyVec<T>: Default`
|
note: trait bound `T: Default` was not satisfied
--> src\main.rs:15:10
|
15 | #[derive(Default, Clone, PartialEq)]
| ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
help: consider restricting the type parameter to satisfy the trait bound
|
6 | fn values<T: Clone>(value: T, len: usize) -> MyVec<T> where T: Default {
| ++++++++++++++++
『HRTB - 型推論』で紹介する通り、HRTB を含んだ型は型推論できない。
そのため、型推論により型が決定された変数は、HRTB を境界とする実装に適合しない。
以下では、MyType<T>::method を呼ぶには、型パラメタ T が HRTB を含んだ境界を満たす必要がある。しかし、_ による型推論は、通常のライフタイム注釈しか推論できない。
fn main() {
let var = MyType(|_| {});
var.method();
}
struct MyType<T>(T);
impl<T> MyType<T> where T: Fn(&i32) {
fn method(&self) {}
}
error[E0599]: no method named `method` found for struct `MyType<{closure@src\main.rs:2:22: 2:25}>` in the current scope
--> src\main.rs:3:9
|
3 | var.method();
| ^^^^^^ method not found in `MyType<{closure@main.rs:2:22}>`
...
6 | struct MyType<T>(T);
| ---------------- method `method` not found for this struct
|
= note: the method was found for
- `MyType<T>`
HRTB を期待する箇所の参照を推論せずに明示するとよい。
fn main() {
let var = MyType(|_arg: &_| {});
var.method();
}
struct MyType<T>(T);
impl<T> MyType<T> where T: Fn(&i32) {
fn method(&self) {}
}