title: Possible? Rust macro to define a bunch of constants?
body: <p>Let's assume, we want a bunch of constants, associating each square of a chess board with its coordinates, so we can use those constants in our Rust code.</p>
  <p>One such definition could be:</p>
  <pre><code>#[allow(dead_code)]
  const A1: (usize,usize) = (0, 0);
  </code></pre>
  <p>and there would be 64 of them.</p>
  <p>Now, as a emacs user, I could generate the source code easily, for example with:</p>
  <pre class="lang-lisp prettyprint-override"><code>(dolist (col '(?A ?B ?C ?D ?E ?F ?G ?H))
    (dolist (row '(?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8))
      (insert &quot;#[allow(dead_code)]&quot;)
      (end-of-line)
      (newline-and-indent)
      (insert &quot;const &quot; col row &quot;: (usize,usize) = (&quot;
          (format &quot;%d&quot; (- col ?A))
          &quot;, &quot;
          (format &quot;%d&quot; (- row ?1))
          &quot;);&quot;)
      (end-of-line)
      (newline-and-indent)))
  </code></pre>
  <p>With the drawback, that now my file just grew by 128 exceptionally boring lines.</p>
  <p>In Common Lisp, I would solve this aspect, by defining myself a macro, for example:</p>
  <pre class="lang-lisp prettyprint-override"><code>(defmacro defconst-square-names ()
         (labels ((square-name (row col)
                (intern 
                 (format nil &quot;+~C~D+&quot; 
                     (code-char (+ (char-code #\A) col))
                     (+ row 1))))
              (one-square (row col)
                `(defconstant ,(square-name row col)
               (cons ,row ,col))))
           `(eval-when (:compile-toplevel :load-toplevel :execute)
          ,@(loop
             for col below 8
             appending 
             (loop for row below 8
               collecting (one-square row col))))))
  (defconst-square-names) ;; nicer packaging of those 64 boring lines...
  </code></pre>
  <p>Now, the question arises, of course,</p>
  <ul>
  <li>if Rust macro system is able to accomplish this?</li>
  <li>can someone show such a macro?</li>
  <li>I read, you need to put such Rust macro into a separate crate or whatnot?!</li>
  </ul>
  <h3>UPDATE</h3>
  <p>@aedm pointed me with the comment about <code>seq-macro</code> crate to my first attempt to get it done. But unfortunately, from skimming over various Rust documents about macros, I still don't know how to define and call compile time functions from within such a macro:</p>
  <pre><code>fn const_name(index:usize) -&gt; String {
    format!(&quot;{}{}&quot;,
        char::from_u32('A' as u32
               + (index as u32 % 8)).unwrap()
        , index / 8)
  }
  
  seq!(index in 0..64 {
    #[allow(dead_code)]
    const $crate::const_name(index) : (usize,usize) = ($(index / 8), $(index %8));
  });
  </code></pre>
  <p>In my Common Lisp solution, I just defined local functions within the macro to get such things done. What is the Rust way?</p>