Skip to content

Commit

Permalink
update traits section
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcandav committed Aug 8, 2023
1 parent 9b6ac65 commit d84bb81
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/traits/index.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Traits
Traits can be used to represent common behaviors. As a result of the implementation of a trait by one or more structures that exhibit common behavior, the trait it applies to becomes usable wherever it is used. A trait can only contain functions. Every function declaration is automatically public and marked as `pub`. A trait can be `nil`
Traits can be used to represent common behaviors. As a result of the implementation of a trait by one or more structures that exhibit common behavior, the trait it applies to becomes usable wherever it is used. A trait can only contain functions. A trait can be `nil`

## Define a Trait
Functions in a trait should only exist as prototypes (declaration of a trait, not a definition).
Expand All @@ -13,5 +13,17 @@ trait Person {
```
All constructs that implement the trait above, must have the methods `name() str` and `age() u8`.

## Access Modifiers

Traits supports access modifiers for definitions. And access modifier of relevant define, should implemented as same.

For example:
```
trait Person {
pub fn name(): str
pub fn age(): u8
}
```

## Comparing Traits
During a comparison of traits: equal traits return true if two traits use the same allocation (both have the same pointer address), false if not.

0 comments on commit d84bb81

Please sign in to comment.