[rust-dev] static mut and owning pointers

François-Xavier Bourlet bombela at gmail.com
Tue Jan 28 13:00:21 PST 2014


match global_data {
            Some(data) =>

You should be able to do:
  Some(ref data)

Which will take a reference instead of moving the owned pointer.

In the second case (no owned pointer) you are actually copying the
struct everything. Using "ref data" would safe a copy as well.

I might be wrong, I am a newcomer on rust :)

On Tue, Jan 28, 2014 at 11:48 AM, Alexander Stavonin
<a.stavonin at gmail.com> wrote:
> Hi all! I'm not sure is it an error or "static mut" variables
> misunderstanding from my side. The source:
>
> struct MyStruct {
>     val: int
> }
>
> static mut global_data: Option<~MyStruct> = None;
>
> fn test_call() {
>     unsafe {
>         match global_data {
>             Some(data) => { println!("We have data {:?}", data);}
>             None => { println!("We don't have data");}
>         }
>     }
> }
>
> fn main() {
>
>     unsafe {
>         global_data = Some(~MyStruct{val: 42});
>     }
>
>     test_call();
>     test_call();
> }
>
> and output:
>
> We have data ~MyStruct{val: 42}
> We don't have data
>
> But if I'm changing global_data from Option<~MyStruct> to Option<MyStruct>
> output is changed also:
>
> We have data ~MyStruct{val: 42}
> We have data ~MyStruct{val: 42}
>
> Is it normal behaviour and owning pointers cannot be stored in global
> variables or an error?
>
> _______________________________________________
> Rust-dev mailing list
> Rust-dev at mozilla.org
> https://mail.mozilla.org/listinfo/rust-dev
>


More information about the Rust-dev mailing list