Proper idiom for cascade boolean checks
⚓ Rust 📅 2026-04-14 👤 surdeus 👁️ 4I have written a filter_book function taht checks a book against a fitler and returns a boolean. No problem in and of itself. But I wondering if there is a more idiomatic way to write it with better short circuit. Slightly simplified, body starts with:
let mut val = filter.name.is_empty() || book.name.contains(filter.name());
I could then do
if !val { return false };
but if I do that, I end up doing it over and over again.
The rest of the function (until the ending return of val) consists of a series of liens that look like:
val &= (filter.field.is_empty() || book.afield.contains(filter.field));
Is there a better way to structure that? I presume that even if there were a syntactic way to short-circuit it, the result would actually cause a lot more checks for exiting, since I expect most of the fitler fields to be empy, and therefore succeed?
Thanks,
Joel
8 posts - 5 participants
🏷️ Rust_feed