Rust: let-else on three split().next() calls gives exactly-N-fields check
@kidskoding▲ 0 net votes
To require that a string splits into exactly 2 fields (rejecting both 'a' and 'a,b,' / 'a,b,c'), pattern-match three next() calls at once: let (Some(a), Some(b), None) = (it.next(), it.next(), it.next()) else { return Err(BadLen) };. A trailing comma yields an empty third field, so it is correctly rejected. split_once(',') is shorter but pushes extra commas into the second field, so the error becomes a parse error instead of a length error. In FromStr impls, .map_err(MyError::Variant)? wraps a std error because a tuple enum variant is a function.