rust/tests/ui/impl-trait/in-trait/success.rs

44 lines
710 B
Rust
Raw Normal View History

2022-08-30 23:46:54 -05:00
// check-pass
2023-09-13 11:04:42 -05:00
#![feature(lint_reasons)]
2022-08-30 23:46:54 -05:00
#![allow(incomplete_features)]
use std::fmt::Display;
pub trait Foo {
2022-08-30 23:46:54 -05:00
fn bar(&self) -> impl Display;
}
impl Foo for i32 {
#[expect(refining_impl_trait)]
2022-08-30 23:46:54 -05:00
fn bar(&self) -> i32 {
*self
}
}
impl Foo for &'static str {
#[expect(refining_impl_trait)]
2022-08-30 23:46:54 -05:00
fn bar(&self) -> &'static str {
*self
}
}
pub struct Yay;
2022-08-30 23:46:54 -05:00
impl Foo for Yay {
#[expect(refining_impl_trait)]
2022-08-30 23:46:54 -05:00
fn bar(&self) -> String {
String::from(":^)")
}
}
fn foo_generically<T: Foo>(t: T) {
println!("{}", t.bar());
}
fn main() {
println!("{}", "Hello, world.".bar());
println!("The answer is {}!", 42.bar());
foo_generically(Yay);
}