49 lines
610 B
Plaintext
49 lines
610 B
Plaintext
---
|
|
title: Traits & Impls
|
|
description: Defining traits, trait bounds, and implementation blocks.
|
|
icon: Puzzle
|
|
---
|
|
|
|
### Traits
|
|
|
|
```mist
|
|
pub trait Drawable
|
|
{
|
|
void draw(&self);
|
|
}
|
|
|
|
pub trait Comparable<T> : Eq
|
|
{
|
|
i32 cmp(&self, T other);
|
|
}
|
|
```
|
|
|
|
Trait requirements are specified after `:`:
|
|
|
|
```mist
|
|
trait MyTrait : SuperTrait + OtherTrait
|
|
{
|
|
void required_method(&self);
|
|
void another(&self);
|
|
}
|
|
```
|
|
|
|
### Impl Blocks
|
|
|
|
```mist
|
|
impl MyType
|
|
{
|
|
void method(&self) { }
|
|
}
|
|
|
|
impl Trait for MyType
|
|
{
|
|
void method(&self) { }
|
|
}
|
|
|
|
impl<T> GenericTrait<T> for MyType
|
|
{
|
|
void method(&self, T value) { }
|
|
}
|
|
```
|