• rust学习~slice迭代器


    背景

    pub fn iter(&self) -> Iter<'_, T>
    
    • 1

    查看Iter

    结构体

    pub struct Iter<'a, T>
    where
        T: 'a,
    {
        /* private fields */ }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    对迭代器求和 sum

    fn sum<S>(self) -> S
    where
        Self: Sized, // 该函数只能在具有已知大小的类型上调用
        S: Sum<Self::Item>, // 指定和的类型,要求和的类型必须实现了 Sum trait
    
    • 1
    • 2
    • 3
    • 4

    该函数返回 self 中所有项的和

    • Self 上的 Sized trait bound 表示该函数只能在具有已知大小的类型上调用,即只能在编译时确定大小的类型上调用该函数。
    • S 类型参数用于指定和的类型,而且要求和的类型必须实现了 Sum trait(这个 trait 用于进行求和操作)。

    求和中的Sum Trait

    std::iter::Sum
    pub trait Sum<A = Self>: Sized {
       
        // Required method
        fn sum<I>(iter: I) -> Self
           where I: Iterator<Item = A>;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    查看int32的Sum实现

    // i32 的实现中,实现了如下trait
    impl Sum<i32> for i32
    
    fn sum<I>(iter: I) -> i32
    where
        I: Iterator<Item = i32>,
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    阅读代码

    // 具体实现方式
    // 一个宏,它接受一系列整数类型作为参数,并为每个整数类型生成函数
    // 这些函数可以计算相应整数类型的一组数字的和与积
    integer_sum_product! {
        i8 i16 i32 i64 i128 isize u8 u16 u32 u64 u128 usize }
    
    // 宏的详细代码
    // 为一系列整数类型实现 Sum 和 Product trait
    macro_rules! integer_sum_product {
       
        // @impls 部分
        // 它是一个递归宏,用于为每个整数类型生成函数的实现
        // @impls 部分以两个参数 $zero 和 $one 开头,这两个参数分别是求和和求积操作的初始值
        // #[$attr:meta] 是一个标识求和和求积方法稳定性的元数据。这部分可以根据具体需求进行修改或省略
        // 为每个整数类型 $a 生成 Sum trait 的实现,初始值为 $zero
        // 为每个整数类型 $a 生成 Product trait 的实现,初始值为 $one
        (@impls $zero:expr, $one:expr, #[$attr:meta], $($a:ty)*) => ($(
            #[$attr]
            impl Sum for $a {
       
                fn sum<I: Iterator<Item=Self>>(iter: I) -> Self {
       
                    // sum 方法的实现使用了 fold 方法来迭代累积计算和
                    // 初始值为 $zero
                    // 并在每次迭代中使用 #[rustc_inherit_overflow_checks] 注解来进行溢出检查
                    // 迭代时,进行求和
                    iter.fold(
                        $zero,
                        #[rustc_inherit_overflow_checks]
                        |a, b| a + b,
                    )
                }
            }
    
            #[$attr]
            impl Product for $a {
       
                fn product<I: Iterator<Item=Self>>(iter: I) -> Self {
       
                    iter.fold(
                        $one,
                        #[rustc_inherit_overflow_checks]
                        |a, b| a * b,
                    )
                }
            }
    
            // 为引用类型生成的实现,允许对 &Self 进行求和
            #[$attr]
            impl<'a> Sum<&'a $a> for $a {
       
                fn sum<I: Iterator<Item=&'a Self>>(iter: I) -> Self {
       
                    iter.fold(
                        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
  • 相关阅读:
    通过继承定义 Layer 和 Model
    Infineon+EB构建MCAL驱动包Demo实现片内外设使用
    ARM架构 --- MMU与协处理器CP15
    谷歌浏览器报错:VM108:5 crbug/1173575, non-JS module files deprecated.
    快速打字只需要16.8
    kubernetes 亲和、反亲和、污点、容忍
    通俗理解ABP中的模块Module
    解析csv文件 流数据问题
    江苏全面推进双重预防数字化建设,强化落实危化企业主体责任
    buildadmin+tp8表格操作(3)----表头上方按钮绑定事件处理,实现功能(选中或取消指定行)
  • 原文地址:https://blog.csdn.net/wangkai6666/article/details/133971688