速成课第5节-练习解答
练习1
fn double(x: &mut u32) {
*x *= 2;
}
fn main() {
let mut x = 5;
double(&mut x);
println!("{}", x);
}练习2
struct InfiniteUnit;
impl IntoIterator for InfiniteUnit {
type Item = ();
type IntoIter = InfiniteUnitIter;
fn into_iter(self) -> Self::IntoIter {
InfiniteUnitIter
}
}
struct InfiniteUnitIter;
impl Iterator for InfiniteUnitIter {
type Item = ();
fn next(&mut self) -> Option<()> {
Some(())
}
}
fn main() {
let mut count = 0;
for _ in InfiniteUnit {
count += 1;
println!("count == {}", count);
if count >= 5 {
break;
}
}
}练习3
练习4
练习5
练习6
练习7
最后更新于