title: How to initialize dynamic size static array in Rust?
body: <p>I have a large static array of structs and I don't want to specify the length (it changes during development).</p>
  <p>When I compile this code:</p>
  <pre><code>#[derive(Copy, Clone)]
  struct A {
      a: i32,
      b: i32,
  }
  
  static ARRAY: [A] = [A { a: 1, b: 2 }, A { a: 3, b: 4 }];
  
  fn main() {
      for item in ARRAY {
          println!(&quot;a: {}, b: {}&quot;, item.a, item.b);
      }
  }
  </code></pre>
  <p>I get the following error:</p>
  <pre class="lang-none prettyprint-override"><code>--&gt; src/main.rs:6:15
    |
  6 | static ARRAY: [A] = [
    |               ^^^ doesn't have a size known at compile-time
    |
    = help: the trait `Sized` is not implemented for `[A]`
  </code></pre>
  <p>Why can't the compiler handle this?</p>
  <p>If I specify the length of the array, it compiles. But I don't want to do that.</p>
  <pre><code>static ARRAY: [A; 2] = [
  </code></pre>
  <p>How can I initialize <code>ARRAY</code> during compile time without specifying the length of <code>ARRAY</code>? I know about the <code>lazy_static</code> macro, but I prefer if I can solve this basic scenario without using any external crates.</p>