Hone logo
Hone
Problems

Const Evaluation Engine in Rust

This challenge asks you to build a simple constant evaluation engine in Rust. The goal is to take a small subset of Rust-like expressions (containing only integer literals, addition, and subtraction) and evaluate them at compile time, leveraging Rust's const functionality. This is useful for generating code based on compile-time calculations, optimizing performance, and enabling more expressive compile-time logic.

Problem Description

You need to implement a function evaluate_expression that takes a string representing a simple arithmetic expression and returns the integer result of evaluating that expression. The expression will consist of integer literals (e.g., 1, 10, 42), addition (+), and subtraction (-) operators. The expression will be well-formed, meaning it will be syntactically valid and follow the order of operations (left to right). The function should use Rust's const functionality to perform the evaluation at compile time.

Key Requirements:

  • Compile-Time Evaluation: The evaluation must happen at compile time. Using runtime evaluation will be considered incorrect.
  • Supported Operations: Only integer literals, addition, and subtraction are supported.
  • Well-Formed Expressions: The input string will always be a valid expression. No error handling for invalid input is required.
  • Left-to-Right Evaluation: Expressions are evaluated from left to right.
  • Integer Literals: Integer literals can be positive or negative.

Expected Behavior:

The evaluate_expression function should parse the input string, perform the arithmetic operations, and return the final integer result as a const.

Edge Cases to Consider:

  • Expressions with only a single integer literal.
  • Expressions with consecutive operators (e.g., 1 + -2).
  • Expressions with negative integer literals (e.g., -5 + 3).
  • Expressions with a mix of positive and negative numbers.

Examples

Example 1:

Input: "1 + 2 + 3"
Output: 6
Explanation: The expression is evaluated as (1 + 2) + 3 = 3 + 3 = 6.

Example 2:

Input: "10 - 5 - 2"
Output: 3
Explanation: The expression is evaluated as (10 - 5) - 2 = 5 - 2 = 3.

Example 3:

Input: "-5 + 3"
Output: -2
Explanation: The expression is evaluated as -5 + 3 = -2.

Example 4:

Input: "42"
Output: 42
Explanation: A single integer literal is returned as is.

Constraints

  • Input Length: The length of the input string will be between 1 and 50 characters (inclusive).
  • Integer Range: Integer literals will be within the range of i32.
  • No Error Handling: The input is guaranteed to be a valid expression. No error handling is required.
  • Compile-Time: The evaluation must be performed at compile time. Using runtime evaluation will result in a failed submission.

Notes

  • Rust's const keyword is essential for achieving compile-time evaluation.
  • Consider using procedural macros to generate the const expression at compile time. This is the recommended approach.
  • You can use Rust's built-in syn and quote crates to parse and generate Rust code.
  • Think about how to represent the expression as a Rust const expression that can be evaluated by the compiler. A simple approach is to directly translate the input string into a Rust expression.
  • The challenge focuses on the evaluation aspect, not the parsing. Assume the input is already a valid expression string.
Loading editor...
rust