1 Star 0 Fork 1

linux内核和设计研究/rust-by-example

forked from alanding/rust-by-example 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
src
attribute
cargo
compatibility
conversion
crates
custom_types
error
flow_control
fn
generics
hello
macros
meta
mod
primitives
scope
std
std_misc
testing
trait
types
variable_bindings
SUMMARY.md
attribute.md
cargo.md
compatibility.md
conversion.md
crates.md
custom_types.md
error.md
expression.md
flow_control.md
fn.md
generics.md
hello.md
index.md
macros.md
meta.md
mod.md
primitives.md
scope.md
std.md
std_misc.md
testing.md
trait.md
types.md
unsafe.md
variable_bindings.md
.gitattributes
.gitignore
.travis.yml
CODE_OF_CONDUCT.md
CONTRIBUTING.md
LICENSE-APACHE
LICENSE-MIT
README.md
book.toml
克隆/下载
trait.md 2.04 KB
一键复制 编辑 原始数据 按行查看 历史
steveklabnik 提交于 8年前 . chapter: traits

Traits

A trait is a collection of methods defined for an unknown type: Self. They can access other methods declared in the same trait.

Traits can be implemented for any data type. In the example below, we define Animal, a group of methods. The Animal trait is then implemented for the Sheep data type, allowing the use of methods from Animal with a Sheep.

struct Sheep { naked: bool, name: &'static str }

trait Animal {
    // Static method signature; `Self` refers to the implementor type.
    fn new(name: &'static str) -> Self;

    // Instance method signatures; these will return a string.
    fn name(&self) -> &'static str;
    fn noise(&self) -> &'static str;

    // Traits can provide default method definitions.
    fn talk(&self) {
        println!("{} says {}", self.name(), self.noise());
    }
}

impl Sheep {
    fn is_naked(&self) -> bool {
        self.naked
    }

    fn shear(&mut self) {
        if self.is_naked() {
            // Implementor methods can use the implementor's trait methods.
            println!("{} is already naked...", self.name());
        } else {
            println!("{} gets a haircut!", self.name);

            self.naked = true;
        }
    }
}

// Implement the `Animal` trait for `Sheep`.
impl Animal for Sheep {
    // `Self` is the implementor type: `Sheep`.
    fn new(name: &'static str) -> Sheep {
        Sheep { name: name, naked: false }
    }

    fn name(&self) -> &'static str {
        self.name
    }

    fn noise(&self) -> &'static str {
        if self.is_naked() {
            "baaaaah?"
        } else {
            "baaaaah!"
        }
    }
    
    // Default trait methods can be overridden.
    fn talk(&self) {
        // For example, we can add some quiet contemplation.
        println!("{} pauses briefly... {}", self.name, self.noise());
    }
}

fn main() {
    // Type annotation is necessary in this case.
    let mut dolly: Sheep = Animal::new("Dolly");
    // TODO ^ Try removing the type annotations.

    dolly.talk();
    dolly.shear();
    dolly.talk();
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/rustlangbase/rust-by-example.git
git@gitee.com:rustlangbase/rust-by-example.git
rustlangbase
rust-by-example
rust-by-example
master

搜索帮助