[rust-dev] Lifetime help

benjamin adamson adamson.benjamin at gmail.com
Mon Jan 20 21:49:21 PST 2014


Ah, the returned sprite must be annotated with the return value as well,
specifically the Sprite (because it has the reference to the Texture).
Thanks Vladimir*! *The gist you provided was very insightful :)

Adding the lifetime annotation to the sprite returned, tells the compiler
the sprite returned in the HashMap will live as long as the texture living
in the assets.

I think I worded that correctly.


On Mon, Jan 20, 2014 at 7:12 AM, Vladimir Matveev <dpx.infinity at gmail.com>wrote:

> In general it is likely that type annotations will be needed. But in
> this case you're quite correct. I have updated the gist, it now has
> significantly less noise.
>
> 2014/1/20 Evan G <eg1290 at gmail.com>:
> > Do you need to type annotate the intermediate variable? Shouldn't "let
> > sprites = assets.iter()" be sufficient? Or is it because of the
> lifetimes?
> >
> > On Jan 20, 2014 1:41 AM, "Vladimir Matveev" <dpx.infinity at gmail.com>
> wrote:
> >>
> >> Hi,
> >>
> >> In fact, you're almost there. You only need to add lifetime
> >> annotations on `Sprite` in the intermediate HashMap variable and in
> >> return type. See here: https://gist.github.com/dpx-infinity/8516387
> >>
> >> I tried to emulate rust-sfml types. This gist compiles, and the test
> >> is successful. BTW, you don't have to write `return` unless you want
> >> to exit early, for example, from inside a loop. Just leave out
> >> semicolon at the last line.
> >>
> >> Simple hint to detect mistakes in lifetime annotations: if you only
> >> have lifetime annotations on parameters or return values but not on
> >> both parameters and return values, then it is likely that something is
> >> wrong.
> >>
> >> 2014/1/20 benjamin adamson <adamson.benjamin at gmail.com>:
> >> > Hi all!
> >> >
> >> > I having a little trouble resolving this issue on my own, and I think
> >> > it's
> >> > an issue of syntax, not so much comprehending life times.
> >> >
> >> > What I'm doing is, using the RSFML library to try and write a pong
> clone
> >> > in
> >> > rust. So far it's been going awesome! I'm running into a small problem
> >> > with
> >> > lifetimes though. To give some context, SFML separates textures from
> >> > sprites, and sprites can be created with a borrowed pointer to a
> >> > texture. I
> >> > first load all the textures into a HashMap, and pass that hashmap to a
> >> > function which (is supposed to) will return a HashMap of Sprites.
> These
> >> > sprites I would like to initialize inside the function with references
> >> > to
> >> > the textures in the input HashMap of textures.
> >> >
> >> > Based on my understanding of borrowed pointers, this should be able to
> >> > work.
> >> > In fact, I wrote a simpler version of my function just to make sure I
> >> > understand what I am not understanding. I wrote a version of the
> >> > function
> >> > which takes the same HashMap of textures, and returns a single sprite
> >> > (not a
> >> > hashmap). The single sprite is created with a reference to the first
> >> > texture
> >> > in the hashmap. I was able to figure out the syntax to express this.
> >> > However
> >> > I can't seem to figure out the syntax for the more complicated case,
> >> > returning the hashmap of Sprites.
> >> >
> >> > I created a gist for easy viewing of the code I am having trouble
> with,
> >> > and
> >> > I think I've made it very obvious what's happening in the gist link,
> >> > hopefully somebody can help me sort this out :D
> >> > https://gist.github.com/ShortStomp/8513368
> >> >
> >> > also the link to the full file, if it helps.
> >> > https://github.com/ShortStomp/pong-rust/blob/master/main.rs
> >> >
> >> > Thanks in advance!!
> >> >
> >> > _______________________________________________
> >> > Rust-dev mailing list
> >> > Rust-dev at mozilla.org
> >> > https://mail.mozilla.org/listinfo/rust-dev
> >> >
> >> _______________________________________________
> >> Rust-dev mailing list
> >> Rust-dev at mozilla.org
> >> https://mail.mozilla.org/listinfo/rust-dev
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140120/57762f69/attachment.html>


More information about the Rust-dev mailing list