mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-04-02 09:44:11 +02:00
21 lines
436 B
Markdown
21 lines
436 B
Markdown
|
# `impl Trait`
|
||
|
|
||
|
Similar to trait bounds, an `impl Trait` syntax can be used in function
|
||
|
arguments and return values:
|
||
|
|
||
|
```rust,editable
|
||
|
use std::fmt::Display;
|
||
|
|
||
|
fn get_x(name: impl Display) -> impl Display {
|
||
|
format!("Hello {name}")
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
let x = get_x("foo");
|
||
|
println!("{x}");
|
||
|
}
|
||
|
```
|
||
|
|
||
|
* `impl Trait` cannot be used with the `::<>` turbo fish syntax.
|
||
|
* `impl Trait` allows you to work with types which you cannot name.
|