本文簡要介紹rust語言中 Function std::fs::copy 的用法。
用法
pub fn copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<u64>將一個文件的內容複製到另一個文件。此函數還將原始文件的權限位複製到目標文件。
該函數將覆蓋的內容to.
請注意,如果 from 和 to 都指向同一個文件,則該文件可能會被此操作截斷。
成功後,將返回複製的總字節數,它等於 metadata 報告的 to 文件的長度。
如果您想將一個文件的內容複製到另一個文件並且您正在使用 File s,請參閱 io::copy() 函數。
特定於平台的行為
此函數當前對應於 Unix 中的 open 函數,其中 O_RDONLY 對應 from ,O_WRONLY 、 O_CREAT 和 O_TRUNC 對應 to 。 O_CLOEXEC 是為返回的文件說明符設置的。在 Windows 上,此函數當前對應於 CopyFileEx 。該函數會複製備用 NTFS 流,但僅返回主流的大小。在 MacOS 上,此函數對應於 fclonefileat 和 fcopyfile 。請注意,這個may change in the future。
錯誤
此函數會在以下情況下返回錯誤,但不僅限於這些情況:
from既不是常規文件,也不是常規文件的符號鏈接。from不存在。- 當前進程沒有讀取
from或寫入to的權限。
例子
use std::fs;
fn main() -> std::io::Result<()> {
fs::copy("foo.txt", "bar.txt")?; // Copy foo.txt to bar.txt
Ok(())
}相關用法
- Rust copy_nonoverlapping用法及代碼示例
- Rust concat用法及代碼示例
- Rust compile_error用法及代碼示例
- Rust column用法及代碼示例
- Rust compiler_fence用法及代碼示例
- Rust concat_idents用法及代碼示例
- Rust char.is_control用法及代碼示例
- Rust char.is_alphanumeric用法及代碼示例
- Rust char.len_utf16用法及代碼示例
- Rust char.is_digit用法及代碼示例
- Rust char.is_ascii_graphic用法及代碼示例
- Rust char.decode_utf16用法及代碼示例
- Rust char.is_uppercase用法及代碼示例
- Rust catch_unwind用法及代碼示例
- Rust char.to_ascii_lowercase用法及代碼示例
- Rust char.is_ascii_uppercase用法及代碼示例
- Rust canonicalize用法及代碼示例
- Rust create_dir用法及代碼示例
- Rust ctlz用法及代碼示例
- Rust channel用法及代碼示例
- Rust char.escape_unicode用法及代碼示例
- Rust char.is_alphabetic用法及代碼示例
- Rust char.is_ascii_control用法及代碼示例
- Rust char.from_u32_unchecked用法及代碼示例
- Rust char.is_ascii_alphabetic用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Function std::fs::copy。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
