Learn Crystal - 10 Code Examples & CST Typing Practice Test
Crystal is a modern, statically typed, compiled programming language with syntax heavily inspired by Ruby. It aims to combine the efficiency and speed of compiled languages with the readability and productivity of Ruby, supporting type inference, concurrency, and high-performance applications.
Learn CRYSTAL with Real Code Examples
Updated Nov 20, 2025
Code Sample Descriptions
Crystal Counter and Theme Toggle
count = 0
is_dark = false
def update_ui(count, is_dark)
puts "Counter: #{count}"
puts "Theme: #{is_dark ? "Dark" : "Light"}"
end
def increment(count, is_dark)
count += 1
update_ui(count, is_dark)
count
end
def decrement(count, is_dark)
count -= 1
update_ui(count, is_dark)
count
end
def reset(count, is_dark)
count = 0
update_ui(count, is_dark)
count
end
def toggle_theme(is_dark)
is_dark = !is_dark
update_ui(count, is_dark)
is_dark
end
# Simulate actions
update_ui(count, is_dark)
count = increment(count, is_dark)
count = increment(count, is_dark)
is_dark = toggle_theme(is_dark)
count = decrement(count, is_dark)
count = reset(count, is_dark)
Demonstrates a simple counter with theme toggling using Crystal variables and console output.
Crystal Simple Addition
def add(a, b)
a + b
end
puts add(10, 20)
Adds two numbers and prints the result.
Crystal Factorial
def factorial(n)
return 1 if n <= 1
n * factorial(n - 1)
end
puts factorial(5)
Calculates factorial recursively.
Crystal Fibonacci Sequence
def fib(n)
return n if n < 2
fib(n - 1) + fib(n - 2)
end
10.times do |i|
puts fib(i)
end
Generates first 10 Fibonacci numbers.
Crystal Max of Two Numbers
def max(a, b)
a > b ? a : b
end
puts max(10, 20)
Finds the maximum of two numbers.
Crystal Even Numbers Filter
arr = [1,2,3,4,5]
arr.each do |x|
puts x if x.even?
end
Prints even numbers from an array.
Crystal Conditional Counter Increment
count = 3
count += 1 if count < 5
puts count
Increment counter only if less than 5.
Crystal Resettable Counter
count = 0
count += 1
count += 1
puts count
count = 0
puts count
Counter that increments and can be reset.
Crystal Theme Toggle Only
is_dark = false
puts is_dark ? "Dark" : "Light"
is_dark = !is_dark
puts is_dark ? "Dark" : "Light"
is_dark = !is_dark
puts is_dark ? "Dark" : "Light"
Toggles theme multiple times.
Frequently Asked Questions about Crystal
What is Crystal?
Crystal is a modern, statically typed, compiled programming language with syntax heavily inspired by Ruby. It aims to combine the efficiency and speed of compiled languages with the readability and productivity of Ruby, supporting type inference, concurrency, and high-performance applications.
What are the primary use cases for Crystal?
Web applications (via Kemal, Amber frameworks). Command-line tools. Microservices and APIs. High-performance backend services. System utilities and scripting. Prototyping with production-ready performance
What are the strengths of Crystal?
High performance due to compilation to native code. Readable Ruby-like syntax. Strong static typing with minimal verbosity. Macros for metaprogramming and code reuse. Good concurrency model with lightweight fibers
What are the limitations of Crystal?
Smaller ecosystem than Ruby or Python. Slower compiler for large projects. Limited libraries for certain domains. Less community support compared to mainstream languages. Still maturing in tooling and IDE support
How can I practice Crystal typing speed?
CodeSpeedTest offers 10+ real Crystal code examples for typing practice. You can measure your WPM, track accuracy, and improve your coding speed with guided exercises.