Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I declare a static mutable variable without assignment?

I tried the following

struct mbuf
{
  cacheline: *mut [u64],               // great amount of rows follows below
  // ..........
}
static mut arr: [mbuf; 32];                // Q1 my main aim
// something as before but using Vec;      // Q2 also main aim

fn main() {
 // let static mut arr: [mbuf; 32];        // Q3 also doesn't work
 // static mut arr: [mbuf; 32];            // Q3 also doesn't work
}

and got error

src/main.rs:74:29: 74:30 error: expected one of `+` or `=`, found `;`
src/main.rs:74   static mut arr: [mbuf; 32];
                                           ^

Q1,Q2,Q3 - Is it possible and how?

like image 242
maki Avatar asked May 09 '15 22:05

maki


1 Answers

A static or constant must be assigned when declared; they can never be assigned to after that.

A static must be purely literals; it cannot have any function calls.

A constant must at present be purely literals, but when RFC 911, const fn is implemented it will be possible to do things more like how you desire.

Inside a function, you can have static or const items, just as outside a function, and there is no difference—placing items (trait and type definitions, functions, &c.) inside a function purely hides them from the outside scope. Therefore you typically might as well use let foo.

like image 168
Chris Morgan Avatar answered Oct 01 '22 22:10

Chris Morgan