std-function

fs::copy: copy a file

Copy file data and permission bits. The destination may be overwritten; source and destination must not refer to the same file. Use a fresh disposable working directory exclusively controlled by this program. Expected stdout (not executed): 3 true Common mistake: Treating copy as a complete preservation of all metadata.

Formal Syntax & Structure

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

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"abc").unwrap();    let q = p.with_file_name("backup.txt");    let n = fs::copy(&p, &q).unwrap();    println!("{} {}", n, fs::read(&q).unwrap() == b"abc");}
Rust Reference