Modules
Import statements import the whole module and assign it to a variable. Specific things from the module cannot be imported, such as JavaScript’s import { function } from pkg or Python’s from pkg import function. Wildcard imports like Python’s from pkg import * are also not allowed.
import { "std/math" as math }
float phi = (math.sqrt(5.0) + 1.0) / 2.0This is beneficial because different modules can export functions with the same name, and it’ll always be clear where each function came from, at the cost of typing slightly more.
Each file is a module, and you can import from files by using the import keyword, and assign it to a variable with as. For example, you could create the modules:
pub uint MIN = 0pub uint MAX = 0xffffffffffffffffpub float PI = 3.14159265358979323
// random number generatorint A = 8121int C = 28411int M = 134456mut int seed = 123456789pub fn random_lcg() -> int { seed = (A * seed + C) % M return seed}You would then be able to use this as:
import { "lib/math" as math "lib/uint" as uint}
@println("pi = {math.PI}") // pi = 3.14159265358979323@println("{uint.MIN} <= uint <= {uint.MAX}") // 0 <= uint <= ...@println(math.random_lcg()) // 69376Exporting symbols
Section titled “Exporting symbols”As noted in the example above, symbols can be exported using the pub keyword. This includes variables, functions, etc.