Saltar al contenido

mod y use

En esta página

mod foo le dice al compilador: "compila y vincula el archivo foo.zolo del mismo directorio". El módulo queda accesible con el prefijo foo::, pero los nombres todavía no entran al ámbito local — eso es trabajo de use.

La forma más directa trae un solo nombre — útil cuando solo necesitas una función del módulo:

use lib_demo::greet importa únicamente greet; el resto del módulo no contamina el ámbito.

02-single-import.zolo
Playground
// Feature: import a single name from the module

// Syntax: `use foo::name` — `name` becomes available without prefix

// When to use: you only need one function/constant from that module;

// avoids polluting the scope with everything.


mod lib_demo

// We only need `greet` here — we ignore the rest.

use lib_demo::greet

fn main() {
  greet("zolo")

  // expected: Hello, zolo!


  greet("world")
  // expected: Hello, world!

}

Para traer varios nombres a la vez, usa la forma de lista con llaves:

use foo::{a, b, c} es más legible que tres use separados cuando los nombres provienen del mismo módulo.

03-list-import.zolo
Playground
// Feature: import several names as a list

// Syntax: `use foo::{a, b, c}` — all become accessible without prefix

// When to use: you need multiple things from the same module, but

// not all of them; more readable than multiple `use foo::name`.


mod lib_demo

// Imports three names at once.

use lib_demo::{add, sub, greet}

fn main() {
  greet("operations")

  // expected: Hello, operations!


  print("10 + 3 = {add(10, 3)}")

  // expected: 10 + 3 = 13


  print("10 - 3 = {sub(10, 3)}")
  // expected: 10 - 3 = 7

}

El siguiente ejemplo combina mod y la importación por lista para acceder a funciones y una constante pública:

mod lib_demo carga el archivo; use lib_demo::{greet, add, get_pi, VERSION} pone los cuatro nombres disponibles sin prefijo.

01-mod-and-use.zolo
Playground
// Feature: declare a module with `mod` and use it via qualified path

// Syntax: `mod foo` — registers `foo.zolo` as module `foo`

// `use foo` + `foo::name` — call with prefix

// When to use: most explicit form; good when you want to make

// clear which module each name comes from.


// Loads the lib_demo.zolo file as a module.

mod lib_demo

// SKIP: `use lib_demo` (whole-module qualified access) currently

// fails to load — the lowering treats it as `use lib_demo::lib_demo`

// and complains about a missing `''` module. Use destructured names

// (`use lib_demo::greet`) or a list import instead, and call them

// directly without the prefix.

use lib_demo::{greet, add, get_pi, VERSION}

fn main() {
  greet("zolo")

  // expected: Hello, zolo!


  let s = add(2, 3)
  print("sum = {s}")

  // expected: sum = 5


  print("pi = {get_pi()}")

  // expected: pi = 3.14159


  print("version = {VERSION}")
  // expected: version = 1.0.0

}

Desafío

Añade sub a la lista de importación del último ejemplo e imprime sub(10, 3). ¿Qué ocurre si intentas usar un nombre que no está en la lista?

Consulta también

Buscar en Zolo

9 resultados

enespt-br