I want to use rust to move a file(or folder) from folder A to another folder, this is my code look like:
use std::{fs, path::PathBuf};
fn main() {
let mut file_path = PathBuf::from("/Users/xiaoqiangjiang/apps/a");
let mut destination_path = PathBuf::from("/Users/xiaoqiangjiang/apps/test/");
let result = fs::rename(file_path, destination_path);
if let Err(err) = result {
print!("{}",err);
}
}
a is a plain text file. when I run this code in Linux(CentOS 7.8)/macOS(13.3.1 (a) ), shows error:
Is a directory (os error 21)%
Am I missing something? I have read the docs and example from web, it seems could not work like this. what should I do to fixed this issue? This is the rust version info:
> rustup -V
rustup 1.27.0 (bbb9276d2 2024-03-08)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.77.1 (7cf61ebde 2024-03-27)`
According to the code snippet, you provide,
file_pathis a file whiledestination_pathis a directory. This is not permitted in rust on linux. According to the docs:Instead you can do this: