Created
April 22, 2024 00:13
-
-
Save thewh1teagle/8c5e12f3b275a393401a34bd0b287f8d to your computer and use it in GitHub Desktop.
temp path in rust
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use eyre::{bail, Context, Result}; | |
use privilege::user::privileged; | |
use rand::distributions::Alphanumeric; | |
use rand::{thread_rng, Rng}; | |
use std::path::PathBuf; | |
pub fn temp_path(prefix: &str, suffix: &str, rand_len: usize) -> PathBuf { | |
let random_string: String = thread_rng() | |
.sample_iter(&Alphanumeric) | |
.take(rand_len) | |
.map(char::from) // From link above, this is needed in later versions | |
.collect(); | |
let name = format!("{}{}{}", prefix, random_string, suffix); | |
let temp_path = std::env::temp_dir(); | |
temp_path.join(name) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment