These are chat archives for rust-lang/rust

28th
Mar 2017
Hollin Wilkins
@hollinwilkins
Mar 28 2017 16:43
Hey everyone, is there a way to map values over a vector and move them instead of having to clone them? I have a Vec<Vec<f64>> and I want to turn it into a Vec<StructContainingVec<f64>> without cloning the vectors
Jacob Kiesel
@Xaeroxe
Mar 28 2017 16:45
@Viewtiful move semantics should do that by default. Just pass in Vec without any & or & mut
Hollin Wilkins
@hollinwilkins
Mar 28 2017 16:46
.iter() returns &T not T though
Michal 'vorner' Vaner
@vorner
Mar 28 2017 16:47
There's something like .drain() or .into_iter().
Jacob Kiesel
@Xaeroxe
Mar 28 2017 16:47
(I missed that you were mapping it) What's the closure you're passing to map? I'm not sure if what you're asking is possible
Hollin Wilkins
@hollinwilkins
Mar 28 2017 16:48
Thanks, looks like IntoIter will do it
Oh wait...
Hmmmm, maybe it won't
Drain will though
Thank you @vorner and @Xaeroxe
Scott Corbeil
@scorbeil
Mar 28 2017 16:52
I'm fighting the borrow checker really hard right now. How do I write a parameter that would accept a function, a closure, or even a method? Here's a silly example I'm playing with to try and get it to work. The piece I can't get write is in the Some variant of the State enum: https://is.gd/NtA68C
can't get right*
Hollin Wilkins
@hollinwilkins
Mar 28 2017 16:53
Check out how the Map iterator does it: https://doc.rust-lang.org/src/core/iter/mod.rs.html#994
Also, potentially doing this will fix: enum State<'a, T: 'a>

Actually, this brings up another question I have been having. What does a lifetime bound on a type parameter mean? I see it in a few places, like when I store a slice reference in a struct:

pub struct AStruct<'a, T: 'a> {
  buf: &'a [T]
}

What does the lifetime bound on T mean?

Jonas Platte
@jplatte
Mar 28 2017 16:58
@hollinwilkins That T doesn't contain any references with a longer lifetime than 'a.
Hollin Wilkins
@hollinwilkins
Mar 28 2017 16:58
Gotcha
So Box<Any + 'static> means that it is an Any that doesn't contain anything with a lifetime longer than 'static?
Jonas Platte
@jplatte
Mar 28 2017 16:59
T + 'a is a little different from T: 'a I think
Generally the Syntax index in the Rust book is a great refernce for this kind of stuff.
Hollin Wilkins
@hollinwilkins
Mar 28 2017 17:00
Cool, I'll have a look
Jonas Platte
@jplatte
Mar 28 2017 17:01
In the case of T + 'a it doesn't seem very useful though. Links to a page where it isn't explained :(
But actually I think it is the same thing, for different contexts
T: 'a is used in generic bounds, Trait + 'a is used in types
Hollin Wilkins
@hollinwilkins
Mar 28 2017 17:03
Yup, makes sense if that's how it works
Thank you!
Scott Corbeil
@scorbeil
Mar 28 2017 17:07
Now I'm getting trait bound error... ideas? https://is.gd/0TjxQs
Still trying to get my head around these lifetimes :\
Hollin Wilkins
@hollinwilkins
Mar 28 2017 17:08
Use a generic that has a bound on it with FnMut(T) -> State<T>
Scott Corbeil
@scorbeil
Mar 28 2017 17:08
Still not compiling
Hollin Wilkins
@hollinwilkins
Mar 28 2017 17:09
enum State<T, F: FnMut(T) -> State<T>> {
    Some(F),
    None,
}
Scott Corbeil
@scorbeil
Mar 28 2017 17:10
blob
Hollin Wilkins
@hollinwilkins
Mar 28 2017 17:11
Yeah, I realize now that won't work for you
Jonas Platte
@jplatte
Mar 28 2017 17:18
@scorbeil What are you trying to do exactly? What @hollinwilkins suggested can be made to work, but will be unergonomic unless you use the conservative_impl_trait feature (requires a nightly compiler like all unstable features)
Ther alternative would be to use Box<FnMut(T) -> State<T>> in your Some, which would mean dynamic dispatch
Scott Corbeil
@scorbeil
Mar 28 2017 17:19
What I'm trying to do is define that enum ("State"), such that the contents of the "Some" variant can accept a method, in addition to functions and closures.
Jonas Platte
@jplatte
Mar 28 2017 17:20
Are you familiar with the distinction between static and dynamic dispatch?
Scott Corbeil
@scorbeil
Mar 28 2017 17:21
As I understand it is the distinction between looking up the function at compile time or runtime, yes?
Jonas Platte
@jplatte
Mar 28 2017 17:21
Yeah
Do you care about that?
Scott Corbeil
@scorbeil
Mar 28 2017 17:22
Not sure. This is sort of an experiment. I'm trying to port a little parser to better learn Rust.
In other words, this isn't going in any critical production code. It'll probably never get used for anything but my own learning.
Jonas Platte
@jplatte
Mar 28 2017 17:22
Okay yeah then you should probably use the box solution
Seen as impl trait isn't stable yet, and I guess it might also be a new concept whereas Box is more or less just an opaque type
Scott Corbeil
@scorbeil
Mar 28 2017 17:23
It's just a performance concern, right?
Jonas Platte
@jplatte
Mar 28 2017 17:23
The advantage of static dispatch is usually better performance, yes
There are disadvantages too, though
Scott Corbeil
@scorbeil
Mar 28 2017 17:24
Although, in the interest of learning the language, I probably ought to learn the "right" way to get this working, eh? haha
Jonas Platte
@jplatte
Mar 28 2017 17:24
There is not always one right thing
Scott Corbeil
@scorbeil
Mar 28 2017 17:25
Btw, when I wrap it in a box, I get this...
Jonas Platte
@jplatte
Mar 28 2017 17:25
It might be that impl trait would be more idiomatic in this context, but it's unstable anyway, hardly something you need to know right now
Yeah I'm trying it myself right now and I'm confused as to what I'm doing wrong :D
Scott Corbeil
@scorbeil
Mar 28 2017 17:25
blob
Jonas Platte
@jplatte
Mar 28 2017 17:26
Oh yeah there you'd have State::Some(Box::new(v.first)), but that also doesn't work, I'm looking at it right now...
Scott Corbeil
@scorbeil
Mar 28 2017 17:27
Here's a working example (that does not accept methods) to perhaps better illustrate what I'm trying to accomplish:
Jonas Platte
@jplatte
Mar 28 2017 17:28
Oh wait, of course...
I know the problem
You can't use member functions like that
v.first is not a value that you can use
|x| v.first(x) should do what you want
(although will pbbly still not work here :D)
Scott Corbeil
@scorbeil
Mar 28 2017 17:31
Nope, sure doesn't lol
Jonas Platte
@jplatte
Mar 28 2017 17:37
I'm sure if you explain the actual thing you're trying to accomplish by passing around these functions, there will be a much nicer solution..
In any case, I should go now (hackerspace day!); might be online here again in 10 minutes, or maybe I won't be online here for the rest of the day. Dunno ^^
Here's how far I got by just stupidly fixing compiler errors: https://is.gd/oQq5fJ
Scott Corbeil
@scorbeil
Mar 28 2017 18:07
Honestly, the reason I was trying to get it to work with methods is so that I could - if desired - provide some contextual state to the functions acting as the states. This just makes it very un-ergonomic. I'd think I'd rather have the functions take a second argument for a context object than jump through all these hoops.
Sheesh, sorry for all the poor grammar.. I'm doing many things at once lol