Run and build
crystal run <file.cr>
Compile and run a file
crystal run <file.cr> -- <args>
Pass arguments to the program
Everything after -- goes to your program, not the compiler.
crystal <file.cr>
Shorthand for run
crystal build <file.cr>
Build a binary in the current dir
crystal build --release <file.cr>
Optimized build (-O3, single module)
crystal build -o <path> <file.cr>
Choose the output path
crystal build --no-debug <file.cr>
Omit debug symbols
crystal build --static <file.cr>
Link statically
Works best on Alpine/musl; glibc static linking is limited.
crystal build -O<level> <file.cr>
Set optimization level 0-3, s, z
crystal build -D <flag> <file.cr>
Define a compile-time flag
crystal build --link-flags <flags>
<file.cr>
Extra linker flags
crystal build --cross-compile <file.cr>
Emit .o and print the link command
crystal build --progress <file.cr>
Show compile progress
crystal build --verbose <file.cr>
Print the commands it executes
Specs
crystal spec
Run all specs in spec/
crystal spec <file>
Run one spec file
crystal spec <file>:<line>
Run the example at a line
crystal spec -e '<name>'
Run examples matching a name
crystal spec --tag <tag>
Run examples with a tag
Prefix the tag with ~ to exclude it.
crystal spec --fail-fast
Stop at the first failure
crystal spec --order random
Randomize example order
crystal spec --profile
Show the 10 slowest examples
crystal spec -v
Verbose, one line per example
crystal spec --dry-run
List examples without running them
crystal spec --release
Build the specs optimized
Options after -- go to the spec runner; compiler flags such as
--release go before the file list.
crystal tool format
Format all files in place
crystal tool format <path>
Format a file or folder
crystal tool format --check
Fail if anything is unformatted (CI)
crystal tool unreachable <file.cr>
List methods that are never called
crystal tool unreachable --check
Exit nonzero if dead code exists
crystal tool dependencies <file.cr>
Show the file require graph
crystal tool dependencies -f mermaid
Output as tree, flat, dot or mermaid
crystal tool expand / context / types
Macro expansion and type inspection
crystal tool hierarchy / implementations
Type tree and method lookup
crystal docs
Generate API docs into ./docs
crystal docs -o <dir> --project-name
<name>
Choose output dir and name
crystal docs -b <url>
Set the canonical base URL
Shards
shards install
Install deps into lib/ per the lock
shards update
Re-resolve all deps, rewrite shard.lock
shards build
Install deps, build into bin/
shards build <target>
Build one named target
shards list
Show installed versions
shards check
Exit 1 if deps are missing
shards prune
Remove unused deps from lib/
shards install --production
Locked only, no dev deps, fail if out of sync
shards update <name>
Update a single dependency
shard.yml
name: myapp version: 0.1.0 targets: myapp: main: src/myapp.cr dependencies: kemal: github: kemalcr/kemal version: ~> 1.6 development_dependencies: ameba: github: crystal-ameba/ameba
Add a dependency to shard.yml by hand, then run shards install;
there is no shards add command. Then require it by shard name.
shard.override.yml overrides a dependency's source without
editing shard.yml, e.g. to point at a local checkout.
Language basics
Types and methods
name = "Ada" age : Int32 = 36 def greet(who : String) : String "Hi, #{who}" end puts greet(name)
Classes and structs
class User property name : String getter id : Int32 def initialize(@name, @id); end end struct Point getter x : Int32, y : Int32 def initialize(@x, @y); end end
Blocks and collections
[1, 2, 3].map { |n| n * 2 } h = {"a" => 1} h.each do |k, v| puts "#{k}=#{v}" end def twice yield yield end
Nil safety
x = ENV["HOME"]? if x puts x.size end x.try &.size x.not_nil!
require "./file" / require "json"
Load a relative file or stdlib module
puts / p / pp
Print, inspect, pretty inspect
Type? means Type | Nil. The compiler rejects calls on a possible
nil; narrow with if, try, or not_nil! (raises at runtime).
Structs are value types and are copied on assignment; classes
are heap references.