Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 37 ยท Getting Started

What Is Crystal?

Meet Crystal, a compiled language with Ruby-inspired syntax, static types, type inference and native speed. Learn what it is good for and how this course works.

Crystal is a general-purpose programming language that tries to combine two things that rarely come together: code that reads almost like Ruby, and programs that run at the speed of compiled native code. You write short, expressive code without type declarations everywhere, and the compiler works out the types for you, checks them before the program runs, and produces a fast executable.

This lesson explains what Crystal is, how it differs from Ruby, where it fits, and how this course handles the fact that the Crystal compiler cannot run inside your browser on this site.

How this course works. The real Crystal compiler is not available on this site. Every Crystal listing is therefore read-only, and we only show its output when we are certain what it prints. Next to the Crystal code you will often find a Ruby stand-in: a small runnable program, clearly labelled, that demonstrates the same idea with Ruby 4.0. Ruby is not Crystal, so each stand-in says where the two differ. To run real Crystal, install it on your computer in the next lesson.

A language in three lines

Here is a complete Crystal program. It is read-only, because it needs the real compiler.

CRYSTAL
name = "world"
puts "Hello, #{name}!"

Running it prints Hello, world!. There is no main function, no class, no imports, and no type annotation. The compiler still knows that name is a String.

The same lines are valid Ruby. Here is the runnable stand-in; it runs in real Ruby, not Crystal:

Ruby Stand-in: the same idea in Ruby

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

What makes Crystal different from Ruby

Crystal borrowed Ruby's look, but it is a different language with a different engine underneath.

Idea Ruby Crystal
Execution Interpreted by a virtual machine Compiled ahead of time to native machine code
Types Checked while the program runs Checked by the compiler before it runs
Type annotations None (optional external tools) Optional: the compiler infers most types
Null safety nil can appear anywhere and fail at run time nil is its own type; the compiler forces you to handle it
Concurrency Threads, Ractors, fibers Lightweight fibers and channels (like Go)
Metaprogramming Run-time (define_method, method_missing) Compile-time macros
Output A script that needs Ruby installed A single executable file

The most important line is the second one. In Ruby, a typo in a rarely used branch may stay hidden for months until a user hits it. In Crystal, the compiler reads your whole program and refuses to build it if a method does not exist on a value that can reach that call.

Static types without the typing

Crystal is statically typed but type-inferred. You usually do not write types for local variables, and often not for method parameters either.

CRYSTAL
def double(x)
  x * 2
end

puts double(21)      # 42
puts double("ab")    # abab
puts double(1.5)     # 3.0

The method double has no types, yet it works for integers, strings and floats. The compiler creates a separate, fully typed version of double for each type it is called with. You get the flexibility of "duck typing" and the safety of a type checker.

What happens if the type does not support the operation?

CRYSTAL
double(nil)

This does not compile. The error mentions that Nil has no method *. You see it at build time, not in production.

Here is a Ruby stand-in that shows the flexibility half of the story. Ruby will run all three calls, but a call that fails would only fail when it is reached:

Ruby Stand-in: one method, many types

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with gets), it gets the next line from this box.

In Crystal that last call would have been rejected before the program ever started. That is the heart of the difference.

Performance

Because Crystal compiles to native code through LLVM, numeric and string-heavy code often runs many times faster than the same logic in Ruby, and in the same range as C, Go or Rust for many programs. Memory is managed by a garbage collector, so you do not free memory by hand. Programs start quickly, and a compiled web server can serve many thousands of requests per second on a single machine.

The cost of this design is compile time. The compiler analyses the whole program and all the libraries it uses, so a large project can take many seconds to build, and release builds with optimisation take longer. The next lessons show the options that keep the feedback loop fast.

What Crystal is good for

  • Command-line tools that start instantly and ship as one file.
  • Web services and APIs, using frameworks such as Kemal, Amber, Lucky or Athena (see the web chapter as it grows).
  • Background workers and data processing, using fibers for concurrency.
  • Performance-sensitive scripts that outgrew Ruby but where you do not want to learn a very different syntax.

It is less suited to places where the library ecosystem matters most. Crystal's package ecosystem (called shards) is much smaller than those of Ruby, Python or JavaScript, so you may find that a library you need does not exist yet.

Who this course is for

You do not need to know Ruby. This course teaches Crystal from the first line and explains every concept it uses. If you do know Ruby, you will recognise a lot, and we point out the differences as we go, because they are where bugs come from. If you want a refresher on Ruby itself, see the Ruby tutorial.

By the end of the course you will be able to write typed, fast programs, use classes, modules, generics, blocks, macros and fibers, structure a project, and build a small web service.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. When does the Crystal compiler report a call to a method that does not exist on a value?

    Crystal is statically typed: the compiler checks the whole program and refuses to build it if a call cannot work.
  2. 2. Which statement about Crystal's type annotations is true?

    Type inference lets you leave out most annotations while keeping full static checking.
  3. 3. What is a major trade-off of Crystal's whole-program compilation?

    Analysing the whole program and optimising it takes time, so builds are slower than starting a Ruby script, although the resulting program is fast.
  4. 4. On this site, how are Crystal listings shown?

    The Crystal compiler is not available here, so Crystal code is read-only and the runnable examples are labelled Ruby stand-ins.

Summary

  • Crystal is a compiled, statically typed language with Ruby-inspired syntax and native speed.
  • The compiler infers most types, so code stays short while errors are caught before the program runs.
  • nil is a type of its own, which prevents a whole class of run-time crashes.
  • Concurrency uses fibers and channels; metaprogramming uses compile-time macros.
  • The trade-offs are longer compile times and a smaller library ecosystem than Ruby's.
  • On this site Crystal code is read-only and Ruby stand-ins are runnable and labelled.

Next: Installing Crystal and Your First Program