13 releases

Uses new Rust 2024

0.5.12 Jun 27, 2025
0.5.9 Mar 15, 2025
0.5.7 Jul 6, 2023
0.5.5 Sep 16, 2022
0.5.1 Jul 16, 2020

#867 in Procedural macros

Download history

782 downloads per month

BSD-2-Clause

16KB
302 lines

Execute Python code at compile time to generate Rust code.

Example

use ct_python::ct_python;

static SIN_2: f64 = ct_python! {
    from math import sin
    print(sin(2))
};

ct_python! {
    print("type num = f64;")
}

fn main() {
    assert_eq!(num::sin(2.0), SIN_2);
}

How to use

Use the ct_python!{..} macro to generate Rust code from an embedded Python script. The output of the script (print() and anything else through sys.stdout) is captured, and will be parsed and injected as Rust code.

Python Errors

Any syntax errors and runtime exceptions from the Python code will be reported by the Rust compiler as compiler errors.

Syntax issues

Since the Rust tokenizer will tokenize the Python code, some valid Python code is rejected. See the inline-python documentation for details.

Dependencies

~3MB
~57K SLoC