zlacker

[parent] [thread] 3 comments
1. swiftc+(OP)[view] [source] 2025-12-06 08:34:15
In the first example, the match feels extremely overkill. Vec.first() exposes the correct semantic (as does Vec.iter().nth(0) for the more general case), returning an Option.
replies(3): >>miniBi+F6 >>fvncc+aa >>bigstr+k01
2. miniBi+F6[view] [source] 2025-12-06 10:03:30
>>swiftc+(OP)
That's fine but the matching exposes that you should also handle the "has more than one element" case. And in general it's pointing to the idea of "try to not separate checks from code that depends on those checks"
3. fvncc+aa[view] [source] 2025-12-06 10:51:40
>>swiftc+(OP)
Note its possible to write the example more succinctly (while having the same behavior) with:

https://docs.rs/itertools/latest/itertools/trait.Itertools.h...

4. bigstr+k01[view] [source] 2025-12-06 18:38:20
>>swiftc+(OP)
I also think the first example has a solution which is worse than the purported problem it attempts to solve. If you're worried that someone might take the if statement out from around the vec index (I don't think this is actually a concern, but let's say it is for sake of argument), what's to stop someone from taking the match statement out from around your slice access? I can't see any reason why the solution isn't equally as vulnerable to the exact same problem. So the match approach doesn't seem to be adding value, while being much more verbose, and less clear.

As you said, calling first() is a far better approach.

[go to top]