I'm learning Rust and I have the following error message:
note: move occurs because value has type
std::string::String
, which does not implement theCopy
trait
I believe it's because I have some fields in a struct
that are String
(even though I'm borrowing &cfg
- hence the value borrowed here after partial move
?).
Am I correct? Any idea how to fix it instead of using Copy
or is it the only solution?
See below a SSCCE
fn main() {
let file_cmd = String::from("file1.conf");
let cfg = Config {
project: Project::Project1,
env: Environment::Test,
cmd: Command::Command1 { file: file_cmd },
};
match cfg.cmd {
Command::Command1 { file } => {
build_path_file(&cfg, &file);
}
_ => ()
}
}
fn build_path_file(config: &Config, file: &str) -> PathsConf {
let local_path = format!("{}/{}", &config.local_path(), file);
let s3_path = format!("{}/{}", &config.s3_path(), file);
PathsConf {
local: local_path,
s3: s3_path,
}
}
enum Project {
Project1,
Project2,
}
enum Environment {
Test,
Live,
}
enum Command {
Command1 { file: String },
Command2 { file: String },
}
struct Config {
project: Project,
env: Environment,
pub cmd: Command,
}
impl Config {
pub fn new(project: Project, env: Environment, cmd: Command) -> Self {
Self {
project,
env,
cmd,
}
}
pub fn s3_path(&self) -> String {
String::from("s3/config")
}
pub fn local_path(&self) -> String {
String::from("local/config")
}
}
struct PathsConf {
pub local: String,
pub s3: String,
}
The error message:
error[E0382]: borrow of moved value: `cfg`
--> src/main.rs:13:29
|
12 | Command::Command1 { file } => {
| ---- value moved here
13 | build_path_file(&cfg, &file);
| ^^^^ value borrowed here after partial move
|
= note: move occurs because value has type `std::string::String`, which does not implement the `Copy` trait