去年秋冬季已经参加过一次训练营了,今年第二次参加,感觉和去年相比,今年的整体流程更加完善了,相应的训练题目也更加合理了。 本身是非科班出身的,一直以来有一个愿望就是弄清计算机是如何运行的,如何从一个个逻辑门到我们所看到的PC,尤其是在软硬件接口处,OS是软件中距离软硬件接口最近的部分,更像是软件领域的“大管家”,如果说编程是创造的过程,那OS就是编程世界的上帝,上帝为程序员准备好了一切支持,程序员才可以进行创造,所以,OS is charming!每个程序员都应该在创造的基础上更进一步,做自己的god。
// quiz2.rs // // This is a quiz for the following sections: // - Strings // - Vecs // - Move semantics // - Modules // - Enums // // Let's build a little machine in the form of a function. As input, we're going // to give a list of strings and commands. These commands determine what action // is going to be applied to the string. It can either be: // - Uppercase the string // - Trim the string // - Append "bar" to the string a specified amount of times // The exact form of this will be: // - The input is going to be a Vector of a 2-length tuple, // the first element is the string, the second one is the command. // - The output element is going to be a Vector of strings. // // No hints this time!
fnmain() { let numbers = vec![1, 2, 3, 4, 5]; let sum = numbers.iter().fold(0, |acc, &x| acc + x); println!("The sum is: {}", sum); // 输出:The sum is: 15 }
.fold()迭代器方法
1 2 3
fnfold<B, F>(self, init: B, f: F) -> B where F: FnMut(B, Self::Item) -> B,
这里的参数含义是:
init 是初始值,它是要合并的类型的默认值或起始状态。
f 是一个闭包函数,它接受两个参数:累积值(accumulator)和当前迭代的元素,并返回一个新的累积值。