Skip to content

Instantly share code, notes, and snippets.

@thewh1teagle
Created April 22, 2024 00:13
Show Gist options
  • Save thewh1teagle/8c5e12f3b275a393401a34bd0b287f8d to your computer and use it in GitHub Desktop.
Save thewh1teagle/8c5e12f3b275a393401a34bd0b287f8d to your computer and use it in GitHub Desktop.
temp path in rust
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