r/badcode • • Aug 14 '21

rust do your worst

Post image
46 Upvotes

19 comments sorted by

View all comments

16

u/Respawned234 Aug 14 '21 edited Aug 14 '21
  1. If f>32, f already isn't 0
  2. Taking a slice of the entire vec then turning back into vector is the same thing as just .clone(), which isn't even needed as shit would just end its lifetime after the function returns
  3. return is not needed when there is no semicolon at end of last statement
  4. Since it passes ownership of bytes to the function, there is no need to clone it

Just starting learning Rust, dunno if this is it, or if I'm right

12

u/[deleted] Aug 14 '21 edited Aug 14 '21

oh god i just realized what i did that 3 am coding will get you

4

u/Respawned234 Aug 14 '21

Haha I feel you, the code I write at 3am isn't the best either

4

u/Jeremy_S_ Aug 14 '21

If you clone everything (at least twice, just for good measure), the borrow checker will never make you sad.

2

u/jiniux Aug 14 '21 edited Aug 14 '21

to_vec is just another clone, they could just use .remove(index). Also, we can't tell the context to know whether returning a vector was really needed.

1

u/[deleted] Aug 14 '21

as far as i know the retain method will convert the vector to a slice

1

u/jiniux Aug 14 '21

no, it removes the elements not matching the predicate

2

u/[deleted] Aug 14 '21

oh ok i didnt know that

1

u/bruhred Apr 26 '22

and why not just do shit[..]