[rust-dev] Compile-time function evaluation in Rust

Pierre Talbot ptalbot at hyc.io
Tue Jan 28 15:56:20 PST 2014


If we drop macro, I can come up with a "simple" solution inspired by the 
Lisp quote:

// Return a Rust construction string.
#[ctfe(always)]
fn make_rust_construct() -> ~str {
   return ~"5 + 5";
}

#[ctfe(always)]
fn eval(s: ~str) -> ??;

fn main() {
   eval(make_rust_construct());
}

We'd rely on string manipulation rather than on a macro language.

For convenience we could add a special quote function:

#[ctfe(always)]
fn make_rust_construct() -> ~str {
   return quote(5 + 5);
}

With support of the compiler, the macros could be implemented in Rust, 
but this is a work that can only be done after CTFE is implemented. So 
we have all the time to invent new syntax and deliberate on the semantic.

On 01/29/2014 12:28 AM, comex wrote:
> Devil's advocate: CTFE and procedural macros would both allow
> arbitrary Rust code to be evaluated at compile time.  Is there any
> sane way to combine them rather than making them separate islands?
>
> Personally, I want a way to run arbitrary code at compile time that
> can introspect on and possibly generate type information, although
> that's different.



More information about the Rust-dev mailing list