Keyboard shortcuts

Press ← or β†’ to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Variables and Types

Chapter Status: βœ… 100% Working (8/8 examples)

StatusCountExamples
βœ… Working8Ready for production use
🎯 Verified8All examples validated with 7-layer testing
❌ Broken0Known issues, needs fixing
πŸ“‹ Planned0Future roadmap features

Last updated: 2025-10-13 Ruchy version: ruchy v1.84.0

Chapter Status: βœ… 100% Test-Driven (8/8 examples passing) Ruchy Version: v1.84.0 Testing: All examples verified with make test-ch02 and 7-layer validation

The Problem

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.

Test-Driven Examples

Example 1: Basic Integer Variable

This example is tested in tests/ch02-variables/test_01_basic_let.ruchy:


fun main() {
    let x = 42;
    println(x);
}


Output:

42

Example 2: String Variable

This example is tested in tests/ch02-variables/test_02_string_var.ruchy:


fun main() {
    let name = "Ruchy";
    println(name);
}


Output:

Ruchy

Example 3: Multiple Variables and Arithmetic

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

Example 4: Floating-Point Calculations

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

Core Concepts

Variable Declaration with let

  • Use let keyword 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

Type Inference

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

Basic Arithmetic Operations

Verified operators:

  • + Addition
  • - Subtraction (tested in other examples)
  • * Multiplication
  • / Division (tested in other examples)

All arithmetic follows standard precedence rules.

Variable Scope

Variables exist within their defining block:


fun main() {
    let outer = 100;
    // outer is accessible here
    println(outer);
}
// outer is NOT accessible here


Testing Your Code

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.ruchy

Common Patterns

Pattern 1: Simple Calculation

fun main() {
    let value1 = 10;
    let value2 = 20;
    let result = value1 + value2;
    println(result);  // Output: 30
}

Pattern 2: Multi-Step Calculation

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
}

Pattern 3: Named Constants

// Error: βœ— Compilation failed: Compilation failed:
let PI = 3.14159;
let GRAVITY = 9.81;


Type Safety

Ruchy enforces type safety:

  • Can’t mix incompatible types
  • Operations must make sense for the types involved
  • Compiler catches type errors before runtime

Summary

βœ… 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

Exercises

Based on our tested examples, try these:

  1. Exercise 1: Calculate the perimeter of a rectangle (width=10, height=20)
  2. Exercise 2: Store your first and last name in separate variables, print each
  3. Exercise 3: Calculate compound interest: principal=1000, rate=0.05, time=3

Next Steps

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 v1.10.0