#is it safe to rely on operations to short circuit in order to not mess up a program?

6 messages · Page 1 of 1 (latest)

candid moon
#

e.g. ```rs
let items = vec![1, 2, 3];
let mut current = 0;

let next = || {
current += 1;
items[current]
}

let peek = || items[current + 1]

let tag = |i| if peek() == i {next(); true} else {false}

if tag(1) || tag(2) {
// if for whatever reason tag(2) is run, this will fail
assert_eq!(peek(), 3);
}

slim cosmos
#

although keep in mind that logic with mutable state like this is hard to follow

brisk sundial
#

yeah this is kinda mind bending

#

please use iterators :p

candid moon
#

in my actual program i use iterators, but i didn't want to drop all that in here so i smashed this out :)