Rutas (std::path)
En esta página
std::path es una biblioteca de manipulación de cadenas; no toca el disco.
Todas las funciones reciben y devuelven cadenas de ruta, usando el separador
correcto del sistema operativo. Para verificar si una ruta existe o leer su
contenido, usa std::fs.
Unir, separar y extraer componentes
path.join ensambla rutas con el separador nativo. path.dirname y
path.basename dividen la ruta en el último separador. path.ext y path.stem
aíslan la extensión y el nombre sin extensión:
Une partes con join; extrae directorio, nombre base, extensión y stem.
// Feature: path.join / path.dirname / path.basename — path manipulation
// When to use: build cross-platform paths without worrying about / vs \.
use std::path
// path.join(...) glues parts together using the right OS separator.
let p = path.join("home", "user", "docs", "file.txt")
print(p) // expected: home/user/docs/file.txt (or \ on Windows)
// path.dirname -> everything up to the last separator.
print(path.dirname("/var/log/app.log")) // expected: /var/log
// path.basename -> last component of the path.
print(path.basename("/var/log/app.log")) // expected: app.log
// path.ext / path.stem split off the extension.
print(path.ext("report.pdf")) // expected: .pdf (or pdf — depends on impl)
print(path.stem("report.pdf")) // expected: report
// path.separator is "/" on Unix and "\" on Windows.
print(path.separator()) // expected: OS separator
Absoluta, relativa y normalización
path.is_absolute / path.is_relative consultan el tipo de ruta.
path.normalize colapsa . y .. y separadores duplicados. path.resolve
devuelve la ruta absoluta (usando el directorio de trabajo actual si la ruta es
relativa). path.relative(from, to) calcula la ruta relativa entre dos puntos:
Verifica el tipo, normaliza, resuelve y calcula rutas relativas.
// Feature: path.is_absolute / is_relative / normalize / resolve / relative
// When to use: validate inputs, resolve relative paths, deduplicate.
use std::path
// is_absolute / is_relative — query the kind of path.
print(path.is_absolute("/etc/hosts")) // expected: true (false on Windows)
print(path.is_relative("docs/readme")) // expected: true
// normalize collapses "." and ".." and duplicate separators.
print(path.normalize("a/b/../c/./d")) // expected: a/c/d
// resolve returns the absolute path (joins with cwd if relative).
let abs = path.resolve("report.txt")
print(abs.len() > 0) // expected: true
// relative(from, to) computes the relative path from `from` to `to`.
print(path.relative("/var/log", "/var/log/app/error.log"))
// expected: app/error.log
Combinando path con fs para verificar existencia
Las funciones de existencia (exists, is_file, is_dir) pertenecen a std::fs,
no a std::path. Este ejemplo muestra cómo construir una ruta con path.join
y probarla con fs:
Une componentes con path.join y verifica existencia con fs.exists / fs.is_file.
// Feature: path/fs — test for file existence
// When to use: validate user input, decide whether to create a file.
// Note: IO functions (`exists`, `is_file`, `is_dir`) live in the `fs` module,
// not in `path`. `path` is pure string manipulation; `fs` touches the disk.
use std::path
use std::fs
// Build the path with path.join and test with fs.exists.
let target = path.join("examples", "features", "17-stdlib", "path", "README.md")
// fs.exists(path) -> bool.
let here = fs.exists(target)
print(here) // expected: true (when running from project root)
// Other type checks:
// fs.is_file(p) — regular file
// fs.is_dir(p) — directory
print(fs.is_file(target)) // expected: true
print(fs.is_dir("examples/features")) // expected: true
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Consulta también