Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Aaronepower
Feb 20, 2014

Jsor posted:

E: Actually, on a mostly unrelated note, can someone explain Box to me? I mean, I know what it does. It's a lot like a malloc'd pointer in C. I just can never find a place to use it because every time I try it feels like the Box type ends up infecting the return stack of the entire program.

The only use case I've found that requires a Box is doing direct recursive relationships. http://is.gd/z1z4yB

You'd also want to wrap big data structures in a Box, the stack you're given is probably around 2MB. So stuff like images, or if you're doing data visualisation, you'll probably have a huge dataset, which would probably cause a stack overflow.

Adbot
ADBOT LOVES YOU

Aaronepower
Feb 20, 2014

FamDav posted:

super confused; why does

code:

        let a = [1, 2, 3];

        let doubled: Vec<i32> = a.iter()
            .map(|&x| x)
            .collect();

compile whereas

code:

        let a = [1, 2, 3];

        let doubled: Vec<i32> = a.iter()
            .collect();

fails because its trying to build a Vec<&i32>?

.iter() doesn't consume the collection it is based on, which is why it gives references. So you can have the Iterator, and the collection. If don't want the collection anymore, you can call .into_iter(), which will take the collection, and convert it into an Iterator.
The reason the map works where you dereference x, is because i32 has the trait Copy, so when you dereference it, you're implicitly cloning the value. Neither one of these will compile with a non Copyable type such as a String.

Aaronepower fucked around with this message at 02:07 on Feb 22, 2016

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply