I'm playing around with iterators and wrote this code:
fn ident<T>(x:T) -> T {x}
fn mult(x:&u64, y:&u64) -> u64 {
x * y
}
fn add(x: &u64, y: &u64) -> u64 {
x + y
}
pub fn hourglass() -> u64 {
let cells = vec![2;64];
let nodes64 = cells.iter().map(|x| ident(*x));
let nodes32 = nodes64.take(32).zip(nodes64.skip(32)).map(
|(x,y)| mult(&x, &y)
);
let nodes16 = nodes32.take(16).zip(nodes32.skip(16)).map(
|(x,y)| add(&x, &y)
);
let nodes8 = nodes16.take(8).zip(nodes16.skip(8)).map(
|(x,y)| mult(&x, &y)
);
let nodes4 = nodes8.take(4).zip(nodes8.skip(4)).map(
|(x,y)| add(&x, &y)
);
let nodes2 = nodes4.take(2).zip(nodes4.skip(2)).map(
|(x,y)| mult(&x, &y)
);
let chokepoint = nodes2.take(1).zip(nodes2.skip(1)).map(
|(x,y)| add(&x, &y)
);
// chokepoint[0] // error: cannot index a value of type `[12 thousand characters]`
match chokepoint.next() {
None => 2, //impossible
Some(a) => a
}
}
fn main() {
// hourglass() // error mismatched types, after checking all the above
println!("result is: {}",hourglass());
}
It takes up to 40 minutes to compile before failing at the borrow checker. I've commented two errors that will show up in the type checker. The one in main should be a simple check, since hourglass
is declared as returning a value, while main cannot. The previous error prints out a 12 thousand character type for chokepoint
.
I'd love to actually run this to see if most of it compiles away and it runs fast, but I don't know enough to satisfy the borrow checker while taking so long between compile iterations.
Is this expected behavior?