Chapter Status: ✅ 100% Working (8/8 examples)
| Status | Count | Examples |
|---|---|---|
| ✅ Working | 8 | Ready for production use |
| 🎯 Verified | 8 | All examples validated with 7-layer testing |
| ❌ Broken | 0 | Known issues, needs fixing |
| 📋 Planned | 0 | Future roadmap features |
Last updated: 2025-11-03 Ruchy version: ruchy 3.213.0
Chapter Status: ✅ 100% Test-Driven (8/8 examples passing)
Ruchy Version: v3.213.0
Testing: All examples verified with make test-ch02 and 7-layer validation
Programs need to store and manipulate data. Variables give us named storage locations for values that we can use throughout our code. In Ruchy, variables are simple, safe, and work exactly as tested.
This example is tested in tests/ch02-variables/test_01_basic_let.ruchy:
fun main() {
let x = 42;
println(x);
}
Output:
42
This example is tested in tests/ch02-variables/test_02_string_var.ruchy:
fun main() {
let name = "Ruchy";
println(name);
}
Output:
Ruchy
This example is tested in tests/ch02-variables/test_03_multiple_vars.ruchy:
fun main() {
let x = 10;
let y = 20;
let sum = x + y;
println(sum);
}
Output:
30
This example is tested in tests/ch02-variables/test_04_float_vars.ruchy:
fun main() {
let pi = 3.14159;
let radius = 5.0;
let area = pi * radius * radius;
println(area);
}
Output:
78.53975
- Use
letkeyword to create variables - Syntax:
let variable_name = value; - Variables are immutable by default (can't be changed after creation)
- Type is inferred from the value
Ruchy automatically determines types:
42→ integer type (i32)3.14→ floating-point type (f64)"text"→ string type (&str)- No need to explicitly declare types in simple cases
Verified operators:
+Addition-Subtraction (tested in other examples)*Multiplication/Division (tested in other examples)
All arithmetic follows standard precedence rules.
Variables exist within their defining block:
fun main() {
let outer = 100;
// outer is accessible here
println(outer);
}
// outer is NOT accessible here
All examples in this chapter can be verified:
# Test all Chapter 2 examples
make test-ch02
# Test specific example
make test-file FILE=tests/ch02-variables/test_01_basic_let.ruchyfun main() {
let value1 = 10;
let value2 = 20;
let result = value1 + value2;
println(result); // Output: 30
}
fun main() {
let initial_value = 100;
let factor = 2;
let adjustment = 50;
let divisor = 3;
let step1 = initial_value * factor;
let step2 = step1 + adjustment;
let final_result = step2 / divisor;
println(final_result); // Output: 83
}
Ruchy enforces type safety:
- Can't mix incompatible types
- Operations must make sense for the types involved
- Compiler catches type errors before runtime
✅ What Works (Test-Verified):
- Integer variables and arithmetic
- Floating-point variables and calculations
- String variables
- Multiple variable declarations
- Basic arithmetic operations (+, *, and implicitly -, /)
- Type inference
⏳ Not Yet Tested (Future Chapters):
- Mutable variables
- Type annotations
- Complex types (arrays, structs)
- Type conversions
- Constants vs variables
Based on our tested examples, try these:
- Exercise 1: Calculate the perimeter of a rectangle (width=10, height=20)
- Exercise 2: Store your first and last name in separate variables, print each
- Exercise 3: Calculate compound interest: principal=1000, rate=0.05, time=3
In Chapter 3, we'll explore functions - how to create reusable blocks of code with parameters and return values, all verified through test-driven development.
Every example in this chapter has been tested and verified to work with Ruchy v3.213.0