Skip to content
This repository was archived by the owner on Oct 17, 2023. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions docs/language_concepts/12_traits.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
title: Traits
description:
Learn how to write traits in Noir programming language. A trait is a group of methods that are defined for a particular type.
keywords: [Noir programming language, traits, rust]
---

Traits in Noir work in [much the same way as in Rust](https://doc.rust-lang.org/book/ch10-02-traits.html). It's highly recommended that you get familiar with the concept of Traits in Rust, as it will help you understanding how they work in Noir.

## Example

First you need to define a Struct that will hold the implementation of a Trait. Example

```rust
struct Values {
x: Field,
y: Field,
}
```

then define the trait:

```rust
trait Comparison {
fn eq(sf: Values, other: Values);
}
```

and the implementation of that trait in your struct:

```rust
impl Comparison for Values {
fn eq(sf: Values, other: Values) {
assert((sf.x == other.x) & (sf.y == other.y));
}
}
```

Now you should have a struct that implements the `Comparison` Trait. This means you can simply call `.eq()` on your struct:

```rust
fn main(x : Values, y : pub Values) {
x.eq(y);
}
```