[rust-dev] Lifetime help

Vladimir Matveev dpx.infinity at gmail.com
Sun Jan 19 23:41:47 PST 2014


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
>


More information about the Rust-dev mailing list