Function in io::fs compiler

rename

fn rename(string from, string to) -> Result<unit, IoError>
Import
use io::fs::{rename};
Source: compiler/src/builtin_docs/io/fs.hy

Renames (moves) from to to, replacing to if it is a file.

Works for files and directories. Moving across file systems fails. Needs --allow-write.

Parameters#

NameType
fromstring
tostring

Returns#

Result<unit, IoError>

Examples#

use io::fs::{exists, remove_file, rename};
use io::file::{write_text};
use tests::assert;
let _ = write_text("/tmp/coil_doc_fs_rename_a.txt", "x");
let moved = match rename("/tmp/coil_doc_fs_rename_a.txt", "/tmp/coil_doc_fs_rename_b.txt") {
    Result::Ok(_) => true,
    Result::Err(_) => false,
};
let new_there = match exists("/tmp/coil_doc_fs_rename_b.txt") {
    Result::Ok(b) => b,
    Result::Err(_) => false,
};
let _ = remove_file("/tmp/coil_doc_fs_rename_b.txt");
assert!(moved && new_there);

See also#

  • copy: Copies the contents of file from to to, creating or overwriting to.
  • create_dir: Creates the directory path.
  • create_dir_all: Creates the directory path and any missing parent directories.
  • exists: Returns whether anything exists at path.
  • is_dir: Returns whether path is a directory, following symbolic links.
  • is_file: Returns whether path is a regular file, following symbolic links.
  • is_symlink: Returns whether path itself is a symbolic link (the link is not followed).
  • list_dir: Returns the names of the entries in directory path, sorted.
↑ ↓ navigate ↵ open