mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-25 08:53:01 +02:00
18 lines
325 B
Markdown
18 lines
325 B
Markdown
|
# Trait Bounds
|
||
|
|
||
|
When working with generics, you often want to limit the types. You can do this
|
||
|
with `T: Trait` or `impl Trait`:
|
||
|
|
||
|
```rust,editable
|
||
|
fn duplicate<T: Clone>(a: T) -> (T, T) {
|
||
|
(a.clone(), a.clone())
|
||
|
}
|
||
|
|
||
|
// struct NotClonable;
|
||
|
|
||
|
fn main() {
|
||
|
let foo = String::from("foo");
|
||
|
let pair = duplicate(foo);
|
||
|
}
|
||
|
```
|