expr!() { /* proc-macro */ }Expand description
Creates an expression at compile time.
This macro allows you to parse expressions during compilation, which provides better performance and catches syntax errors early.
§Examples
use std::collections::HashMap;
use expression_macro::expr;
let expr = expr!("sin(x) + cos(y)");
let mut vars = HashMap::new();
vars.insert("x".to_string(), 0.0);
vars.insert("y".to_string(), 0.0);
let result = expr.evaluate(&vars).unwrap();
assert_eq!(result, 1.0);§Errors
If the expression contains syntax errors, compilation will fail with appropriate error messages.
§Supported Syntax
§Operators
- Addition:
+ - Subtraction:
- - Multiplication:
* - Division:
/ - Exponentiation:
^
§Functions
- Trigonometric:
sin,cos,tan - Inverse trigonometric:
arcsin,arccos,arctan
§Other Features
- Parentheses for grouping:
(expression) - Variables: any alphabetic identifier
- Number literals: both integers and floating-point
§Technical Details
This macro uses the tokenizer and parser from the expression_core crate to
parse the expression string at compile time and generate equivalent Rust code.
The output is an Expression object that can be evaluated with different variable values.