Yes, especially since self.length() - 1 will straight up panic if the stack is empty, while Vec::last will return None. The reason is that self.length() - 1 will try to subtract 1 from an unsigned integer equal to 0. To correct it, you could use wrapping_sub, then it would work correctly by returning None for the index usize::MAX. But of course, the way to do it is to use last ;)
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Instead of
Vec::get
you can useVec::last
.I didn't know that! Thanks!
Yes, especially since
self.length() - 1
will straight up panic if the stack is empty, whileVec::last
will returnNone
. The reason is thatself.length() - 1
will try to subtract 1 from an unsigned integer equal to 0. To correct it, you could use wrapping_sub, then it would work correctly by returningNone
for the indexusize::MAX
. But of course, the way to do it is to uselast
;)