More radical ideas about gc and reference counting

Tommi via Digitalmars-d digitalmars-d at puremagic.com
Mon May 12 01:37:28 PDT 2014


On Monday, 12 May 2014 at 08:10:43 UTC, Tommi wrote:
> Perhaps: [..]

Somewhat surprisingly to me, you can later on change the borrowed 
pointer in the mutable static 'Test' to point at a mutable static 
int:

struct Test {
     n: &'static int
}

static old: int = 111;
static mut new: int = 222;
static mut t: Test = Test { n: &'static old };

fn main() {
     unsafe {
         println!("{}", *t.n); // prints: 111
         t.n = &new; // Can point to a different static
         println!("{}", *t.n); // prints: 222
         // ...but can't point to a local, e.g:
         // let v = 123;
         // t.n = &v; // error: `v` does not live long enough
         new = 333;
         println!("{}", *t.n); // prints: 333
     }
}


More information about the Digitalmars-d mailing list