本頁尚未翻譯,以下為英文原文。
#E0141: method generic parameter named like its impl's
A method written in an impl block declares a generic parameter with the name of one of the impl's (impl<K> W<K> { fn f<K>(..) }): inside the method the name could mean either. Rename the method's parameter (fn f<K2>), or drop it and use the impl's.
nabi
struct Wrap<K> { k: K; }
impl<K> Wrap<K> {
fn pick<K>(self, x: K) -> K { x }
}
error[E0141]: generic parameter `K` of `pick` is already a parameter of its `impl` --> main.nabi:4:5 | 4 | fn pick<K>(self, x: K) -> K { x } | ^^ | = help: rename the method's (`fn pick<K2>`), or drop it to use the impl's `K`
Give the method's parameter its own name:
nabi
struct Wrap<K> { k: K; }
impl<K> Wrap<K> {
fn pick<U>(self, x: U) -> U { x }
}
println(Wrap { k: "s" }.pick(5));
5
A trait's default method is written in the trait, not in the impl, so its parameters shadow an implementing impl's of the same name without an error (2.10.1).
Related: E0026 name defined more than once
Found a mistake? Edit this page on GitHub. Last updated