类似Java中的Interface,可以想象成一组功能或者单个功能的能力标识。
抽象方法,默认方法 use std::fmt::Display;
fn main() {
let person = Person;
let tips = person.tips();
println!("{}", tips);
person.read_book("ohBug".to_string());
}
pub trait ReadThing<T: Display> {
// 抽象方法,供其他类型实现
fn tips(&self) -> String;
// 默认方法实现
fn read_book(&self, msg: T) {
let tips: String = self.tips();
println!("tips {}", tips);
println!("msg: {}", msg);
}
}
struct Person ;
impl ReadThing<String> for Person {
fn tips(&self) -> String {
"Read Tips".to_string()
}
}
//output
Read Tips
tips Read Tips
msg: ohBug
trait类型的方法参数 fn main() {
let person = Person;
tarit_param_method(&person);
}
// 用trait限制方法参数类型
fn tarit_param_method(msg: &impl Read) {
let msg = msg.read();
println!("{}", msg);
}
pub trait Read {
fn read(&self) -> String;
}
struct Person;
impl Read for Person {
fn read(&self) -> String {
"Read for Person".to_string()
}
}
// Read for Person
泛型使用 入参限制 简单限制 use std::fmt::{Display, Formatter};
fn main() {
let man = Man;
let woman = Woman;
man.do_something();
woman.do_something();
sell(&man);
sell(&woman);
sell_two::<Man, Woman>(&man, &woman);
sell_two_trait(&man, &woman);
sell_two_trait(&man, &man);
sell_same_trait(&man, &man); // 仅允许连个类型必须一致
// sell_same_trait(&man, &woman); //error expected `&Man`, but found `&Woman`
}
pub trait DoWork {
fn do_something(&self);
}
struct Man;
struct Woman;
impl DoWork for Man {
fn do_something(&self) {
println!("Man do something")
}
}
impl Display for Man {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "({})", self)
}
}
impl DoWork for Woman {
fn do_something(&self) {
println!("Woman do something")
}
}
fn normal<T>(target: &T) {
println!("coding method");
}
fn sell<T: DoWork>(target: &T) {
println!("sell method");
target.do_something();
}
// 仅能限制住两个参数是都impl了DoWork,并不能限制是相同的类
fn sell_two<T, U>(sell_one: &impl DoWork, sell_two: &impl DoWork) {
println!("sell_two method");
sell_one.do_something();
sell_two.do_something();
}
// 仅能限制住两个参数是都impl了DoWork,并不能限制是相同的类
fn sell_two_trait<T: DoWork, U: DoWork>(sell_one: &T, sell_two: &U) {
println!("fn sell_two_trait method");
sell_one.do_something();
sell_two.do_something();
}
// 这的限制更加的严格,参数必须是相同的类型。而不仅仅是只impl了DoWork就可以
fn sell_same_trait<T: DoWork>(sell_one: &T, sell_two: &T) {
println!("fn sell_same_trait method");
sell_one.do_something();
sell_two.do_something();
}
// T 要求实现了Display + DoWork类型,而U只要求实现了 DoWork
fn work_man<T: Display + DoWork, U: DoWork>(one: T, two: U) {
println!("work_man");
one.do_something();
println!("{}", one.to_string());
two.do_something();
}
Man do something
Woman do something
sell method
Man do something
sell method
Woman do something
sell_two method
Man do something
Woman do something
fn sell_two_trait method
Man do something
Woman do something
fn sell_two_trait method
Man do something
Man do something
fn sell_same_trait method
Man do something
Man do something
多重限制 fn main() {
let man = Man;
let woman = Woman;
work_man(&man, &woman);
}
// T 要求实现了Display + DoWork类型,而U只要求实现了 DoWork
fn work_man<T: Display + DoWork, U: DoWork>(one: &T, two: & U) {
println!("work_man");
one.do_something();
println!("one.to_string() {}", one.to_string());
two.do_something();
}
// output
work_man
Man do something
one.to_string() (Display Man)
Woman do something
where 限制 // T 要求实现了Display + DoWork类型,而U只要求实现了 DoWork
fn work_man<T, U> (one: &T, two: & U) /*(no return)*/ where T: Display + DoWork, U: DoWork {
println!("work_man");
one.do_something();
println!("one.to_string() {}", one.to_string());
two.do_something();
}
// 有返回值的
fn work_man_u32<T, U> (one: &T, two: & U) -> usize where T: Display + DoWork, U: DoWork {
println!("work_man");
one.do_something();
println!("one.to_string() {}", one.to_string());
two.do_something();
one.to_string().len()
}
限制实现impl trait Surgery {
fn do_surgery(&self);
}
struct Person<T> {
_thing: T,
}
// 任何person对象都会有sleep方法
impl<T> Person<T> {
fn sleep(&self) {
println!("person sleep")
}
}
// 只有实现了Surgery trait的Person 才可以拥有surgery方法
impl<T: Surgery> Person<T> {
fn surgery(&self) {
println!("surgery")
}
}
出参限制