From edf57626129467dacc0c6f04b2ca00e64d5b2245 Mon Sep 17 00:00:00 2001 From: mo8it Date: Wed, 24 Apr 2024 01:17:39 +0200 Subject: [PATCH] Preallocate path --- src/embedded.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/embedded.rs b/src/embedded.rs index 2de3b1cf..756b4141 100644 --- a/src/embedded.rs +++ b/src/embedded.rs @@ -43,7 +43,13 @@ struct ExerciseDir { impl ExerciseDir { fn init_on_disk(&self) -> Result<()> { - let dir_path = format!("exercises/{}", self.name); + let path_prefix = "exercises/"; + let readme_path_postfix = "/README.md"; + let mut dir_path = + String::with_capacity(path_prefix.len() + self.name.len() + readme_path_postfix.len()); + dir_path.push_str(path_prefix); + dir_path.push_str(self.name); + if let Err(e) = create_dir(&dir_path) { if e.kind() == io::ErrorKind::AlreadyExists { return Ok(()); @@ -54,8 +60,11 @@ impl ExerciseDir { ); } - WriteStrategy::Overwrite - .write(&format!("exercises/{}/README.md", self.name), self.readme)?; + let readme_path = { + dir_path.push_str(readme_path_postfix); + dir_path + }; + WriteStrategy::Overwrite.write(&readme_path, self.readme)?; Ok(()) }