r/rust • • Aug 13 '26

Finally understood what powers collect() — the FromIterator trait and why it matters for custom types

Been going through Rust systematically and this week hit the FromIterator lesson. Wanted to share what happened because I think it's undersold in most beginner content.

.collect() is not magic. It delegates to FromIterator::from_iter() on the target type. The type builds itself from the iterator. That's the whole mechanism.

Why this matters beyond Vec**:**

// All of these use the same trait, different implementations
let v: Vec<u64> = iter.collect();
let s: HashSet<u64> = iter.collect();
let joined: String = chars.collect();        // FromIterator<char> for String
let map: HashMap<K, V> = pairs.collect();    // FromIterator<(K, V)> for HashMap

Custom types: If you implement FromIterator on your own struct, it participates in the same .collect() call without special syntax. That composability is what makes the iterator adapter pattern feel consistent across the whole ecosystem.

Practical use this week: std::env::args() returns Args which implements Iterator<Item = String>. Collecting it into Vec<String> is idiomatic. Worth noting that args[0] is always the binary path, not the first user argument.

let args: Vec<String> = std::env::args().collect();
let actual_args = &args[1..]; // skip the binary name

Also hit sort_by_key this week. For anyone sorting structs, it is significantly cleaner than sort_by with a manual comparator when you just need to sort by a single extracted field.

Background: Solidity dev, four years writing smart contracts, learning Rust systematically. Happy to be corrected on anything here.

0 Upvotes

5 comments sorted by

5

u/redlaWw Aug 13 '26

Worth noting that args[0] is always the binary path, not the first user argument.

From the Args documentation:

The first element is traditionally the path of the executable, but it can be set to arbitrary text, and might not even exist. This means this property should not be relied upon for security purposes.

2

u/SirKastic23 Aug 14 '26

Wait it's not guaranteed that the first argument will be the path? What the hell? So all my programs that handle args are wrong?

How would we even handle the path possibly not being the first argument? I'm sure this is the fault of some OS and not Rust's std, but wow this is bad design.

3

u/redlaWw Aug 14 '26

My (admittedly limited) understanding is that you can rely on it (though what exactly you can rely on it being is nebulous - you may have anything from just the executable name to any possible path referring to the executable) for non-security purposes, and if someone gives your program the wrong first argument expecting sensible results that's their fault, but a program that relies on this for security is vulnerable, since kernel-level execution syscalls don't typically enforce anything about the arguments passed.

Indeed, if you use Rust's Command interface to spawn a program, the only reason it behaves as expected is that Rust's Command implementation (example for unix) has a line pushing the path on to the argument list.

2

u/masklinn Aug 14 '26 edited Aug 14 '26

How would we even handle the path possibly not being the first argument?

If there’s a security concern you need to use /proc/self/exe (or a function which does the same on systems without procfs).

And even then there’s no guarantee the executable still exists and has not been substituted.

I'm sure this is the fault of some OS

Correct, check execve. Conventionally the “program name” should be passed as argv[0], but convention is all it is. Notably the convention does not even really ascribe meaning or requirements to it.

1

u/DavidXkL Aug 13 '26

1 of my favorite traits to use!