[rust-dev] static mut and owning pointers

Alexander Stavonin a.stavonin at gmail.com
Tue Jan 28 11:48:27 PST 2014


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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140128/63c6a198/attachment.html>


More information about the Rust-dev mailing list