std-function

fs::rename: move within a filesystem

Move an entry to another name. Replacement rules vary by platform; moving across mount points is unsupported. Use a fresh disposable working directory exclusively controlled by this program. Expected stdout (not executed): false draft Common mistake: Assuming rename works between volumes or guarantees crash durability.

Formal Syntax & Structure

rust syntaxrust
fs::rename(from, to) -> io::Result<()>

Working Example

Example Snippetrust
fn main() {    use std::fs::{self, File};    use std::io::{Read, Write};    fs::create_dir("bm-fixture").expect("fresh working directory required");    let p = std::path::Path::new("bm-fixture").join("note.txt");    File::create_new(&p).unwrap().write_all(b"draft").unwrap();    let q = p.with_file_name("final.txt");    fs::rename(&p, &q).unwrap();    println!("{} {}", p.exists(), fs::read_to_string(q).unwrap());}
Rust Reference