Code Examples
Learn BreadCrumbs through practical examples. From beginner to advanced, find code snippets for common tasks.
Hello World
BeginnerThe classic first program - basic syntax and comments
## Multi-line comment
some text
more text
##
#Single-line comment
#[ Documentation comment
It can span multiple lines.
Used for generating markdown documentation in IDE.
]#
# === Top-level statement ===
print("Hello, World!")
# === Default statement ===
func main()
{
print("Hello, World!")
}
#print("Hello, World!") -- errorConditions
BeginnerWorking with if/elif/else statements and ternary operators
## Base conditions usage
if(<condition>) {
...
}
elif(<condition>) {
...
}
else {
...
}
if(<condition>) <value_if_true> elif(<condition>) else <value_if_false>
##
var condition : bool = true
var another_condition : bool = false
if(condition) {
print("message from the if statement")
}
elif(another_condition) {
print("message from the elif statement")
}
else {
print("message from the else statement")
}
#Output: message from the if statement
# === Ternary Operator ===
var result : str = if(condition) "condition is true" else "condition is false"Variables
BeginnerVariable declarations and types
#[ Variable declarations
<mod> <name> [: <type>] = <value>
=== Modifiers ===
var -- Variable that can be reassigned.
const -- Constant variable that cannot be reassigned.
final -- Variable that can be assigned only once.
static -- Variable with static storage duration.
=== Types ===
int -- Integer type.
float -- Floating-point type.
str -- String type.
bool -- Boolean type.
any -- A type that can hold any literal.
=== Literals ===
<int> -- Integer literal (e.g., 42 | 0x2a | 0b0101010 | 0o52).
<float> -- Floating-point literal (e.g., 3.14).
"<string>" -- String literal (e.g., "Hello, World!").
true | false -- Boolean literals.
=== Tuple ===
(<type1>, <type2>, ...) -- Tuple type with specified types.
(var1:<type1>, var2:<type2>, ...) -- Named tuple type.
=== Array ===
<type>[] -- Dynamic array of <type>.
<type>[<size>] -- Static array of <type> with fixed <size>.
.len -- Property to get the length of an array.
]#
# === Inferred types ===
var x = 42 # -- int
var name = "Bread" # -- str
# === Explicit types ===
var age : int = 16
var height : float = 175.5
var is_student : bool = true
# === Multiple declarations ===
var a, b : int = 10, 20 # -- a = 10, b = 20
var (a, b) : int = 52 # -- a = 52, b = 52
# === Any type ===
var any_type : any = 10 # -- any_type = 10
any_type = "string" # -- any_type = "string"
# === Constants ===
const PI : float = 3.14159
const MAX_USERS : int = 1000
# === Final variables ===
final username : str = "admin"
username = "user" # -- error
final score : int
score = 100
score = 200 # -- error
# === Static variables ===
static counter : int = 0
counter = counter + 1
# === Tuples ===
var point : (float, float) = (10.5, 20.5)
var (x, y) : (float, float) = point
var rgb : (r:int, g:int, b:int) = (255, 0, 128)
# === Array ===
var numbers : int[] = {1, 2, 3, 4, 5}
var first_number : int = numbers[0]Loops
BeginnerIteration with for and while loops
#[ Basic loop constructs
while(<condition>) { -- standard while loop
...
}
for(<init>; <condition>; <update>) {-- standard for loop
...
}
for(<var || start>..<iterator>) { -- range-based for loop
...
}
loop { -- infinite loop
...
}
var array : type[] = {...} -- array declaration
array[0..array.len] { -- inline array iteration
...
}
]#
var count : int = 0
while(count < 3) {
print("while loop count: " + count)
count += 1
}
for(var i : int = 0; i < 3; i++) {
print("for loop index: " + i)
}
for(var i..3) {
print("range-based for loop index: " + i)
}
# === Inline array iteration ===
var array : int[] = {1..5}
print(array[0..array.len]) #12345
array[0..array.len] = 0 # [0] - 0, [1] - 0, ...
array[0..array.len] {
print("array element: " + self)
}Imports
IntermediateImporting modules and using external code
#[ Standard library
inout -- input/output operations
system -- system-related functions
format -- string formatting utilities
collection -- data structures and collections
math -- mathematical functions and constants
thread -- threading and concurrency
network -- network-related functionalities
time -- time and date utilities
console -- console input/output operations
memory -- memory management and operations
pointer -- pointer manipulations
debug -- debug utilities
benchmark -- benchmarking utilities
]#
# === Import recursevly ===
import std
# === Inline import ===
import std.inout, std.system, std.format
# === Multiline import ===
import {
std.math,
std.thread
}
# === Import only std modules ===
import std {
network,
console,
time
}
# === Importing std.collection content ===
import std {
collection {
list,
map,
set
}
}
# === Importing external packages installed via package manager ===
import package.raylib
import package.box2d
import package.sqlite
import package.zlibFunctions
IntermediateDefining and calling functions
#[ Function definitions
func <name>(<parameters>) : <return-type>
{
...
}
]#
func simple_function()
{
print("This is a simple function.")
}
func get_float() : float
{
return 3.14
}
func get_string() : str
{
return "Hello, World!"
}
func get_array() : int[5]
{
return int[5]{1, 2, 3, 4, 5}
}
func main()
{
simple_function()
var x : float = get_float()
var y : int = get_string()
var arr : int[5] = get_array()
}
Structs
IntermediateCreating custom data structures
#[ Compound types base usage
<compound-type> <name>
{
...
}
]#
enum Direction
{
UP,
DOWN,
LEFT,
RIGHT
}
struct Person
{
var name : str
var age : int
var height : float
}
func main()
{
var person : Person = {"Bread", 16, 170}
const person_direction : Direction = Direction.NAHUI
}Collections
IntermediateWorking with arrays, maps, and sets
#[ Collections usage
<mod> <name> [: <collection><<generic>>] = {<data>}
=== Collection types ===
list<t> -- A list is an ordered collection of elements of type t.
stack<t> -- A stack is a collection of elements of type t that follows the Last In First Out (LIFO) principle.
queue<t> -- A queue is a collection of elements of type t that follows the First In First Out (FIFO) principle.
map<t,v> -- A map is a collection of key-value pairs, where keys are of type t and values are of type v.
set<t> -- A set is a collection of unique elements of type t.
graph<t> -- A graph is a collection of nodes of type t, where each node may be connected to other nodes.
tree<t> -- A tree is a hierarchical collection of nodes of type t, where each node has a parent-child relationship with other nodes.
=== Methods ===
.add(element : type) -- add element to the collection
.remove(element : type) -- remove element from the collection
.contains(element : type) : bool -- check if the collection contains the element
.is_empty() : bool -- check if the collection is empty
.clear() -- remove all elements from the collection
=== Properties ===
.len : int -- number of elements in the collection
.front : type -- get the first element of the collection
.back : type -- get the last element of the collection
]#
import std.collection
var my_list : list<int> = {1, 2, 3, 4, 5}
var my_stack : stack<str> = {"first", "second", "third"}
var my_queue : queue<float> = {1.1, 2.2, 3.3}
var my_map : map<str, int> = { "one": 1, "two": 2, "three": 3 }
var my_set : set<int> = {1, 2, 3, 4, 5}
var my_graph : graph<str> = { "A": ["B", "C"], "B": ["A", "D"], "C": ["A"], "D": ["B"] }
var my_tree : tree<int> = {1: [2, 3], 2: [4, 5], 3: [], 4: [], 5: []}
print("List length: " + my_list.len) # Output: List length: 5
print("Stack top: " + my_stack.back) # Output: Stack top: third
print("Queue front: " + my_queue.front) # Output: Queue front: 1.1
print("Map value for 'two': " + my_map["two"]) # Output: Map value for 'two': 2
print("Set contains 3: " + my_set.contains(3)) # Output: Set contains 3: true
print("Graph connections for 'A': " + my_graph["A"]) # Output: Graph connections for 'A': ["B", "C"]
print("Tree children of node 2: " + my_tree[2]) # Output: Tree children of node 2: [4, 5]Traits
AdvancedDefining and implementing traits
#[ Traits and implementations
]#Fork/Join
AdvancedConcurrent programming with fork and join
#[ Fork and branching
fork <type> {
branch <name> {
...
}
branch <name> {
...
}
}
##Memory Management
AdvancedUnderstanding memory allocation and deallocation
#[ Base memory API
=== Static methods ===
memory.reserve(size : int) : memory -- reserve memory block
=== Methods ===
.store(element : type) -- store element in memory
.delete(element : type) -- delete element from memory
.split(size : int) : memory -- split memory block into two parts
.extend(size : int) -- increase memory block size
.shrink(size : int) -- decrease memory block size
.release() -- clear all used memory
.compact() -- compact memory block
=== Properties ===
.volume : int -- total size of memory block
.used : int -- currently used memory size
.available : int -- available memory size
.data : type[] -- array of stored elements
]#
import std.memory
var mem : memory = memory.reserve(64)
print(mem.volume) # -- 64 byte
var part_1 = mem.split(mem.volume / 2) #split memory between 'mem' and 'partition'
var hw : str = "hello world"
var x : int = 42
#copy variables to the memory
mem.store(hw)
mem.store(x)
print(mem.used) # -- 12 byte engaged
print(mem.available) # -- 20 byte that you can use
print(mem.data[0]) # -- "hello world"
mem.delete(mem.data[0]) #delete "hello world" from memory
mem.extend(32) #increase to 64 byte
mem.shrink(16) #decrease to 48 byte
mem.compact() #compacting memory: 12 byte available
mem.release() #clearing all of used memory: 12 byte availableNetwork Programming
AdvancedHTTP clients and servers
#[ Network base usage
]#
import std.networkPointers
AdvancedWorking with pointers and references
#[ Base pointer API
=== Methods ===
.ref(element : type) : int -- reference to element address
.set(value : type) : void -- set value at pointer
.offset(offset : int) : ptr<type> -- get pointer offset by given amount
.copy() : ptr<type> -- create a copy of the pointer
.delete() : void -- delete pointer
=== Properties ===
.addr : int -- get address of pointer
.value : type -- get value at pointer
.is_null : bool -- check if pointer is null
.type : type -- get type of pointer
.size : int -- get size of pointer
.align : int -- get alignment of pointer
]#
import std.pointer
var a : int = 42
var ptr_a : ptr<int> = ptr.ref(a)
print($"Address of a: {ptr_a.addr}") #address of a: 0x7ffeedcba098
print($"Value at pointer: {ptr_a}") #value at pointer: 42
var ptr_b : ptr<int> = ptr_a.copy()
ptr_b.set(100)
print($"New value of a: {a}") #new value of a: 100
var ptr_c : ptr<int> = ptr_a.offset(4) #assuming int is 4 bytes
print($"Pointer c address: {ptr_c.addr}") #pointer c address: (address of a + 4)
print($"Is pointer c null? {ptr_c.is_null}") #is pointer c null? false
print($"Pointer a type: {ptr_a.type}") #pointer a type: int
print($"Pointer a size: {ptr_a.size}") #pointer a size: 4
print($"Pointer a alignment: {ptr_a.align}") #pointer a alignment: 4
ptr_a.delete() #delete pointer a
ptr_b.delete() #delete pointer b
ptr_c.delete() #delete pointer cSimulation
AdvancedBuilding simulations and models
#[ Simulating
]#Problem Solving
AdvancedAlgorithmic problem solving patterns
#[ Solving
]#Testing
IntermediateWriting and running tests
#[ Testing
]#