Skip to content

Commit 73e848e

Browse files
author
fmoko
authored
Merge pull request #416 from danwilhelm/traits-readme
feat: Add traits README
2 parents 5f08069 + 173bb14 commit 73e848e

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

exercises/traits/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
### Traits
2+
3+
A trait is a collection of methods.
4+
5+
Data types can implement traits. To do so, the methods making up the trait are defined for the data type. For example, the `String` data type implements the `From<&str>` trait. This allows a user to write `String::from("hello")`.
6+
7+
In this way, traits are somewhat similar to Java interfaces and C++ abstract classes.
8+
9+
Some additional common Rust traits include:
10+
11+
+ `Clone` (the `clone` method),
12+
+ `Display` (which allows formatted display via `{}`), and
13+
+ `Debug` (which allows formatted display via `{:?}`).
14+
15+
Because traits indicate shared behavior between data types, they are useful when writing generics.
16+
17+
18+
#### Book Sections
19+
20+
- [Traits](https://doc.rust-lang.org/book/ch10-02-traits.html)

0 commit comments

Comments
 (0)