docs

你正在阅读 v0.1.2 的文档。Nabi 0.1.2 已不再提供支持。 最新版本:v0.1.3 →

本页尚未翻译,以下为英文原文。

#E0071: trait used as a type

A trait is not a type. For a value of any type that implements it write any Trait, and for a generic parameter T: Trait.

nabi
trait Animal { fn name(self) -> str; }
fn f(a: Animal) {}
error[E0071]: trait `Animal` cannot be used as a type directly
  --> main.nabi:2:9
   |
 2 | fn f(a: Animal) {}
   |         ^^^^^^
   |
   = help: use `any Animal` for a value of any type implementing it, or a generic parameter `<T: Animal>`

Write any Animal:

nabi
trait Animal { fn name(self) -> str; }
struct Dog {}
impl Animal for Dog { fn name(self) -> str { "dog" } }

fn f(a: any Animal) { println(a.name()); }
f(Dog {});
dog

Related: E0070 not a trait, E0069 trait bound not satisfied

Found a mistake? Edit this page on GitHub. Last updated