Closed oilel closed 1 year ago
In this case it does not really matter. Compiler knows length of both values at compile time, so it's able to generate efficient comparison code for simple a[..] == h[..]
(after future migration to const generics you will be able to use a == h
). In general, if you do not have a good reason do not use unsafe
code, doubly so if you are not confident in your knowledge. When you work on micro-optimizations looking at generated assembly (e.g. by using https://rust.godbolt.org/) could be quite informative.
P.S.: I suggest using https://users.rust-lang.org/ for such questions.
Note: This code will panic, but don't care about that, I'm asking about the performance of comparing. The 1st implementation is using Deref operator, the 2nd is using unsafe indexing (.get_unchecked), which is faster when
assert!
?