[rust-dev] static mut and owning pointers
a.stavonin at gmail.com
Tue Jan 28 12:17:56 PST 2014
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.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Rust-dev