官术网_书友最值得收藏!

Traits

Traits are a way to specify that a type must implement some methods and/or some types. They are similar to interfaces in Java. We can implement a trait on a type and we'll be able to use the methods of this trait on this type as long as this trait is imported. This is how we can add methods to types defined in other crates or even the standard library.

Let's write a trait representing a bit set:

trait BitSet {
    fn clear(&mut self, index: usize);
    fn is_set(&self, index: usize) -> bool;
    fn set(&mut self, index: usize);
}

Here, we don't write the body of the methods, as they will be defined when we implement this trait for a type.

Now, let's implement this trait for the u64 type:

impl BitSet for u64 {
    fn clear(&mut self, index: usize) {
        *self &= !(1 << index);
    }

    fn is_set(&self, index: usize) -> bool {
        (*self >> index) & 1 == 1
    }

    fn set(&mut self, index: usize) {
        *self |= 1 << index;
    }
}

As you can see, the bitwise not operator is ! in Rust, as opposed to ~ in other languages. With this code, we can call these methods on u64:

let mut num = 0;
num.set(15);
println!("{}", num.is_set(15));
num.clear(15);

Remember the #[derive(Debug)] attribute? This actually implements the Debug trait on the following type. We could also manually implement the Debug trait on our type, using the same impl syntax, if the default implement does not suit our use case.

主站蜘蛛池模板: 比如县| 龙井市| 司法| 榕江县| 靖宇县| 木兰县| 肇东市| 辽宁省| 喀喇沁旗| 安国市| 巴林右旗| 柞水县| 许昌县| 宁南县| 莎车县| 尼勒克县| 游戏| 丹江口市| 巍山| 武邑县| 卫辉市| 玉屏| 台州市| 荣昌县| 平定县| 沙坪坝区| 静宁县| 汤原县| 固镇县| 中阳县| 厦门市| 虞城县| 定远县| 竹山县| 临沭县| 通城县| 北海市| 阳朔县| 聂荣县| 滕州市| 沾化县|