std::fs::copy - Rust
Description: API documentation for the Rust `copy` fn in crate `std`.
Keywords: rust, rustlang, rust-lang, copy

# 1.0.0[−][src]Function std::fs::copy

    pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64>

Copies the contents of one file to another. This function will also copy the permission bits of the original file to the destination file.

This function will overwrite the contents of `to`.

Note that if `from` and `to` both point to the same file, then the file will likely get truncated by this operation.

On success, the total number of bytes copied is returned and it is equal to the length of the `to` file as reported by `metadata`.

If you’re wanting to copy the contents of one file to another and you’re working with `File`s, see the `io::copy` function.

# Platform-specific behavior

This function currently corresponds to the `open` function in Unix with `O_RDONLY` for `from` and `O_WRONLY`, `O_CREAT`, and `O_TRUNC` for `to`. `O_CLOEXEC` is set for returned file descriptors. On Windows, this function currently corresponds to `CopyFileEx`. Alternate NTFS streams are copied but only the size of the main stream is returned by this function. Note that, this may change in the future.

# Errors

This function will return an error in the following situations, but is not limited to just these cases:

- The `from` path is not a file.
- The `from` file does not exist.
- The current process does not have the permission rights to access `from` or write `to`.

# Examples

    use std::fs;

    fn main() -> std::io::Result<()> {
        fs::copy("foo.txt", "bar.txt")?;  // Copy foo.txt to bar.txt
        Ok(())
    }Run

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)