Pular para o conteúdo

mod e use

Nesta página

mod foo diz ao compilador: "compile e vincule o arquivo foo.zolo que fica no mesmo diretório". O módulo fica acessível pelo prefixo foo::, mas os nomes ainda não entram no escopo local — isso é trabalho do use.

A forma mais direta traz um único nome — útil quando você só precisa de uma função do módulo:

use lib_demo::greet importa apenas greet; o restante do módulo não polui o escopo.

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 trazer vários nomes de uma vez, use a forma de lista com chaves:

use foo::{a, b, c} é mais legível do que três use separados quando os nomes vêm do mesmo 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

}

O exemplo a seguir combina mod e a lista de importação para acessar funções e uma constante pública:

mod lib_demo carrega o arquivo; use lib_demo::{greet, add, get_pi, VERSION} disponibiliza os quatro nomes sem prefixo.

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

}

Desafio

Adicione sub à lista de importação do último exemplo e imprima sub(10, 3). O que acontece se você tentar usar um nome que não está na lista?

Veja também

Buscar no Zolo

9 resultados

enespt-br