[][src]Function tao_of_rust::ch03::trait_limit::trait_limit

pub fn trait_limit()

trait局限

Base usage: 作为父crate在考虑设计trait时,不得不考虑要不要给全体的T或&'a T实现trait 才能不影响到子crate

impl<T:Foo> Bar for T { }
impl<'a,T:Bar> Bar for &'a T { }Run

Base usage:

use std::ops::Add;
#[derive(PartialEq)]
struct Int(i32);
impl Add<i32> for Int {
    type Output = i32;
    fn add(self, other: i32) -> Self::Output {
        (self.0) + other
    }
}
// impl Add<i32> for Option<Int> {
//    // TODO
// }
impl Add<i32> for Box<Int> {
    type Output = i32;
    fn add(self, other: i32) -> Self::Output {
        (self.0) + other
    }
}

assert_eq!(Int(3) + 3, 6);
assert_eq!(Box::new(Int(3)) + 3, 6);Run