Version: 1.11.0 Estimated Time: 5 minutes
Get up and running with Rust Rule Engine in just a few minutes!
Add to your Cargo.toml:
[dependencies]
rust-rule-engine = "1.11"
# Optional features
[dependencies.rust-rule-engine]
version = "1.11"
features = ["backward-chaining", "streaming", "streaming-redis"]| Feature | Description | Use When |
|---|---|---|
backward-chaining |
Goal-driven inference | You need queries and logical reasoning |
streaming |
Complex Event Processing | You process real-time event streams |
streaming-redis |
Redis state backend | You need distributed state management |
use rust_rule_engine::{Engine, Facts, Value};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Create an engine
let mut engine = Engine::new();
// 2. Add a simple rule
engine.add_rule_from_string(r#"
rule "VIP Discount" {
when
Customer.TotalSpent > 1000
then
Customer.DiscountRate = 0.2;
Customer.IsVIP = true;
}
"#)?;
// 3. Set up facts
let mut facts = Facts::new();
facts.set("Customer.TotalSpent", Value::Number(1500.0));
// 4. Run the engine
engine.run(&mut facts)?;
// 5. Check results
assert_eq!(facts.get("Customer.IsVIP"), Some(&Value::Boolean(true)));
assert_eq!(facts.get("Customer.DiscountRate"), Some(&Value::Number(0.2)));
println!("✅ VIP status granted with 20% discount!");
Ok(())
}Output:
✅ VIP status granted with 20% discount!
engine.add_rule_from_string(r#"
rule "Free Shipping" {
when
Customer.IsVIP == true &&
Order.Total > 50
then
Order.ShippingCost = 0;
}
"#)?;
facts.set("Order.Total", Value::Number(75.0));
engine.run(&mut facts)?;Create rules/customer.grl:
rule "VIP Discount" {
when
Customer.TotalSpent > 1000
then
Customer.DiscountRate = 0.2;
Customer.IsVIP = true;
}
rule "Free Shipping" {
when
Customer.IsVIP == true && Order.Total > 50
then
Order.ShippingCost = 0;
}
Load it:
use std::fs;
let grl_content = fs::read_to_string("rules/customer.grl")?;
engine.add_rules_from_grl(&grl_content)?;use rust_rule_engine::backward::BackwardEngine;
let mut bc_engine = BackwardEngine::new(kb);
// Ask a question
let result = bc_engine.query("Customer.IsVIP == true", &facts)?;
if result.provable {
println!("✅ Customer is VIP!");
} else {
println!("❌ Customer is not VIP");
}🎓 Learn Core Concepts → Basic Concepts Understand facts, rules, and pattern matching
⚡ Forward Chaining Deep Dive → Forward Chaining Master the RETE algorithm
🔍 Backward Chaining & Queries → Backward Chaining Quick Start Goal-driven reasoning and queries
🌊 Stream Processing → Streaming Guide Real-time complex event processing
📖 Full API Reference → API Reference Complete API documentation
// Loan approval
rule "Approve Loan" {
when
Applicant.CreditScore > 700 &&
Applicant.Income > 50000 &&
Applicant.DebtRatio < 0.4
then
Loan.Status = "approved";
}// Dynamic pricing
rule "Flash Sale" {
when
Product.Category == "Electronics" &&
Inventory.Stock > 100
then
Product.Price = Product.Price * 0.8;
Product.Label = "Flash Sale!";
}// Treatment authorization
rule "Authorize Treatment" {
when
Patient.InsuranceCoverage == "Premium" &&
Treatment.Cost < 10000
then
Treatment.Authorized = true;
}- Use meaningful fact names -
Customer.TotalSpentinstead ofx.y - Keep rules simple - One rule, one purpose
- Test incrementally - Add rules one at a time
- Use GRL files - Better organization for complex systems
- Enable features as needed - Don't load what you don't use
✅ Check that facts match the condition exactly
✅ Verify fact types (Number vs Integer)
✅ Run engine.run() after setting facts
✅ Check GRL syntax (semicolons, braces) ✅ Ensure field names don't have typos ✅ Use double quotes for strings
// ✅ Good - specific patterns
when Customer.Type == "VIP"
// ❌ Avoid - too broad
when Customer.TotalSpent > 0
// ✅ Use indexing for large rule sets
engine.with_index_on("Customer.Type");- Write Your First Rules - Detailed rule writing guide
- Core Concepts - Deep dive into architecture
- API Reference - Complete API docs
- Examples - Real-world use cases
📚 Documentation Home |