[rust-dev] static mut and owning pointers

Alexander Stavonin a.stavonin at gmail.com
Tue Jan 28 12:17:56 PST 2014


Thanks, Kevin!

On 29 Jan 2014, at 00:12, Kevin Ballard <kevin at sb.org> wrote:

> Your code is moving the contents of Option<~MyStruct> into the match arm. It just so happens that this seems to be zeroing out the original pointer in memory, and that happens to be the same representation that None does for the type Option<~MyStruct> (since ~ pointers are non-nullable), so the act of moving the value just happens to be transforming it into a None.
> 
> Normally you couldn't do this, but mutable statics are weird (which is why you need the unsafe block to access it).
> 
> When you remove the ~, the lines end up printing the same because MyStruct is implicitly copyable, so your match arm is now copying instead of moving.
> 
> The correct fix here is to use `Some(ref data)` instead of `Some(data)`. This will take a reference to the data instead of moving it, and the static will remain unchanged.
> 
> -Kevin

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140129/b3b53552/attachment.html>


More information about the Rust-dev mailing list