impl Trait

ํŠธ๋ ˆ์ดํŠธ ๋ฐ”์šด๋“œ์™€ ์œ ์‚ฌํ•˜๊ฒŒ implํŠธ๋ ˆ์ดํŠธ ๋ฌธ๋ฒ•์€ ํ•จ์ˆ˜์˜ ์ธ์ž์™€ ๋ฐ˜ํ™˜๊ฐ’์— ์ ์šฉ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค:

Similar to trait bounds, an impl Trait syntax can be used in function arguments and return values:

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 ํŠธ๋ ˆ์ดํŠธ๋Š” ํ„ฐ๋ณดํ”ผ์‰ฌ๋ฌธ๋ฒ•(::<>)์—๋Š” ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
  • impl ํŠธ๋ ˆ์ดํŠธ๋Š” ์ต๋ช…ํƒ€์ž…๊ณผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • impl Trait cannot be used with the ::<> turbo fish syntax.
  • impl Trait allows you to work with types which you cannot name.