本文簡要介紹rust語言中 Function std::fs::rename
的用法。
用法
pub fn rename<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> Result<()>
將文件或目錄重命名為新名稱,如果 to
已存在,則替換原始文件。
如果新名稱位於不同的掛載點,這將不起作用。
特定於平台的行為
此函數當前對應於 Unix 上的 rename
函數和 Windows 上帶有 MOVEFILE_REPLACE_EXISTING
標誌的 MoveFileEx
函數。
因此,雙方的行為from
和to
存在不同。在 Unix 上,如果from
是一個目錄,to
也必須是一個(空)目錄。如果from
不是目錄,to
也必須不是目錄。相比之下,在 Windows 上,from
可以是任何東西,但是to
必須不是是一個目錄。
請注意,這個may change in the future。
錯誤
此函數會在以下情況下返回錯誤,但不僅限於這些情況:
from
不存在。- 用戶沒有查看內容的權限。
from
和to
位於不同的文件係統上。
例子
use std::fs;
fn main() -> std::io::Result<()> {
fs::rename("a.txt", "b.txt")?; // Rename a.txt to b.txt
Ok(())
}
相關用法
- Rust remove_dir_all用法及代碼示例
- Rust read用法及代碼示例
- Rust remove_file用法及代碼示例
- Rust read_to_string用法及代碼示例
- Rust read_link用法及代碼示例
- Rust resume_unwind用法及代碼示例
- Rust repeat用法及代碼示例
- Rust ready用法及代碼示例
- Rust read_unaligned用法及代碼示例
- Rust remove_dir用法及代碼示例
- Rust remove_var用法及代碼示例
- Rust read_dir用法及代碼示例
- Rust read_volatile用法及代碼示例
- Rust repeat_with用法及代碼示例
- Rust replace用法及代碼示例
- Rust range用法及代碼示例
- Rust UdpSocket.set_multicast_loop_v6用法及代碼示例
- Rust i64.overflowing_add_unsigned用法及代碼示例
- Rust Box.downcast用法及代碼示例
- Rust BTreeMap.last_key_value用法及代碼示例
- Rust str.make_ascii_uppercase用法及代碼示例
- Rust u128.checked_pow用法及代碼示例
- Rust usize.wrapping_mul用法及代碼示例
- Rust AtomicU8.fetch_sub用法及代碼示例
- Rust PanicInfo.payload用法及代碼示例
注:本文由純淨天空篩選整理自rust-lang.org大神的英文原創作品 Function std::fs::rename。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。