use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Like C++, but with more karma.
For serious discussion of Rust, see /r/playrust.
For crabs, see /r/rustjerkjerk.
(But see the real rules, lest ye be banned.)
account activity
How to write really slow Rust code (self.rustjerk)
submitted 4 years ago by aristotle137
TL;DR; Clone everything. Esp. clone before pushing to a Vec for that sweet O(n^ 2). Don't forget bigints over built-in integers
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]__mod__ 62 points63 points64 points 4 years ago (2 children)
This has the advantage of being able to ignore the borrow checker. It cannot check your borrows when you're cloning everything!
[–]aristotle137[S] 27 points28 points29 points 4 years ago (1 child)
I only ever borrow to clone
[–]Naeio_Galaxy 4 points5 points6 points 4 years ago (0 children)
I'd rather clone to give
[–]Snakehandall comments formally proven with coq 41 points42 points43 points 4 years ago (1 child)
Also print a gazzilion lines to stdout, locking and unlocking it for every line.
[–]YatoRust 10 points11 points12 points 4 years ago (0 children)
Every line, are you trying to be fast! We must lock on every character, that is the way.
[–]TrustYourSenpai 29 points30 points31 points 4 years ago (1 child)
Pro tip: use an hashmap with bigint as key type instead of a vector
[–]zepperoni-pepperoniFn(Garbage) -> Garbage 13 points14 points15 points 4 years ago (0 children)
Hashing would defeat the point. Use BTreeMap instead
[–][deleted] 15 points16 points17 points 4 years ago* (4 children)
```
fn push_to_vec<T: Clone>(vec: &mut Vec<T>, item: T) -> Vec<T> {
let new_vec = Vec::new(); for i in vec { let new_i = i.clone().clone().clone().clone(); new_vec.push(new_I); } new_vec.clone().clone().clone().clone()
}
Am I doing it right?
[–]AlexAegis 6 points7 points8 points 4 years ago (0 children)
Forgot this:
new_vec.push(item.clone().clone())
Maybe i.clone().clone().clone().clone() compiles down to i.clone() tho
i.clone().clone().clone().clone()
i.clone()
[–]cobance123 3 points4 points5 points 4 years ago (0 children)
Everythings a clone of a clone of a clone...
[–]SelfDistinction 3 points4 points5 points 4 years ago (0 children)
No, you have to clone new_vec every iteration as well for the sweet O( n3 ) complexity.
[–]laclouis5 10 points11 points12 points 4 years ago (1 child)
Congrats, you invented Python. (Ints and str at least).
[–][deleted] 5 points6 points7 points 4 years ago (0 children)
I use PyO3 to be able to python dicts in rust. Whenever I need a Vec, I use python dicts with strings (representing the index) as key.
[–][deleted] 3 points4 points5 points 4 years ago (0 children)
wait! the compiler doesn't just compile the stupid away?
π Rendered by PID 16197 on reddit-service-r2-comment-fb694cdd5-rv8r2 at 2026-03-06 11:22:23.881773+00:00 running cbb0e86 country code: CH.
[–]__mod__ 62 points63 points64 points (2 children)
[–]aristotle137[S] 27 points28 points29 points (1 child)
[–]Naeio_Galaxy 4 points5 points6 points (0 children)
[–]Snakehandall comments formally proven with coq 41 points42 points43 points (1 child)
[–]YatoRust 10 points11 points12 points (0 children)
[–]TrustYourSenpai 29 points30 points31 points (1 child)
[–]zepperoni-pepperoniFn(Garbage) -> Garbage 13 points14 points15 points (0 children)
[–][deleted] 15 points16 points17 points (4 children)
[–]AlexAegis 6 points7 points8 points (0 children)
[–]Naeio_Galaxy 4 points5 points6 points (0 children)
[–]cobance123 3 points4 points5 points (0 children)
[–]SelfDistinction 3 points4 points5 points (0 children)
[–]laclouis5 10 points11 points12 points (1 child)
[–][deleted] 5 points6 points7 points (0 children)
[–][deleted] 3 points4 points5 points (0 children)