temporary
import std::fs::temporary; · source
Temporary files and directories, removed when they are dropped.
import std::fs::file::{ File };
import std::fs::temporary::{ TemporaryDirectory };
const scratch: TemporaryDirectory = TemporaryDirectory::create()?;
File::write_text(scratch.path().join("input.txt").as_path(), "data")?;
// ... the directory and everything in it is removed here
Each is created under env::temporary_directory() with a name of random hex digits from SecureRng, and created exclusively — O_EXCL / CREATE_NEW for a file, a failing mkdir for a directory — so two processes never share one. A name already taken is retried with another. keep hands the path over and leaves the entry in place.
Types
TemporaryDirectory
type struct TemporaryDirectory {
private location: PathBuf;
private kept: boolean;
}
A new, empty directory, removed with everything in it when the value is dropped.
Constructors
static create() -> Result<TemporaryDirectory, IoError>;
static create() -> Result<TemporaryDirectory, IoError>;A new, empty directory under the temporary directory.
Methods
path(&this) -> Path;
path(&this) -> Path;Where the directory is.
keep(mut this) -> PathBuf;
keep(mut this) -> PathBuf;Leave the directory in place when this value is dropped, and return its path.
TemporaryFile
type struct TemporaryFile {
private location: PathBuf;
private handle: File;
private kept: boolean;
}
A new, empty file, open for reading and writing, that is closed and removed when the value is dropped. file() borrows the open File to read, write, and seek; keep() closes it and leaves it in place.
import std::fs::temporary::{ TemporaryFile };
mut temp: TemporaryFile = TemporaryFile::create()?;
temp.file().write("scratch")?;
Constructors
static create() -> Result<TemporaryFile, IoError>;
static create() -> Result<TemporaryFile, IoError>;A new, empty file under the temporary directory, open for reading and writing.
Methods
path(&this) -> Path;
path(&this) -> Path;Where the file is.
file(mut &this) -> mut &File;
file(mut &this) -> mut &File;The open file, to read, write and seek.
keep(mut this) -> PathBuf;
keep(mut this) -> PathBuf;Leave the file in place when this value is dropped, and return its path. The file is still closed then.
Trait implementations
TemporaryDirectory
implement trait Drop for struct TemporaryDirectory
TemporaryFile
implement trait Drop for struct TemporaryFile
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
type struct File {}An open file, closed when the value is released. It implements Read and Write; the static methods read or write a whole file in one call.
namespace std::fs::temporary;Temporary files and directories, removed when they are dropped.
module pagetype struct TemporaryDirectory {}A directory that is removed, with everything in it, when this value is dropped.
std::fs::temporaryconst NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
const scratch: TemporaryDirectorystatic TemporaryDirectory::create() -> Result<TemporaryDirectory, IoError>A new, empty directory under the temporary directory.
std::fs::temporaryvalue?Error propagation. On a Result<T, E> it yields the T of an Ok and otherwise returns the Err(e) from the enclosing function unchanged; on an Option<T> it yields the T of a Some or returns None. It works on any type implementing Try, in a function whose return type implements FromResidual of it; an error of another type converts through From.
static File::write_text(path: Path, text: Str) -> Result<(), IoError>Make the file at path hold exactly text, creating it if needed.
const scratch: TemporaryDirectoryPath::join(&this, segment: Str) -> PathBufAppend segment to a COPY of this path, leaving the borrowed path untouched. The non-mutating twin of PathBuf::push, and it inherits push's root semantics: a segment carrying its own root - /etc, C:\dir, \\server\share - replaces rather than extends: join("/etc") is /etc whatever the path was.
PathBuf::as_path(&this) -> PathThis path borrowed as a Path, valid until this one changes or is released.
type struct Name { ... }
type enum Name { ... }
type trait Name { ... }
type union Name { ... }Begins a type declaration; the keyword that follows says which kind. On its own, type Name = Existing; declares an alias.
type struct Name {
field: Type;
method(&this) -> Ret { ... }
}A value type with named fields and optional methods. Structs live on the stack and are passed by value. Fields are public by default and may carry = default values; methods take their receiver as &this, mut &this, or this.
type struct TemporaryDirectory {}A directory that is removed, with everything in it, when this value is dropped.
private type struct Name { ... }
private:Confined to the module that declares it - for items and members alike. A private field or function reached from another module is E0353, and a private type named there is E0503. private: opens a block of such members.
TemporaryDirectory.location: PathBuftype struct PathBuf<A = GlobalAllocator> {}An owned filesystem path that can be extended with push or built by Path.join. It dereferences to Path, so every Path method reads a PathBuf too (file_name, parent, exists, ...); as_path hands the view out.
TemporaryDirectory.kept: booleantype struct TemporaryFile {}A file, open for reading and writing, that is closed and removed when this value is dropped.
TemporaryFile.location: PathBufTemporaryFile.handle: FileTemporaryFile.kept: booleantype struct TemporaryFile {}A file, open for reading and writing, that is closed and removed when this value is dropped.
std::fs::temporarymut name: Type = value;A mutable binding, reassignable after declaration - mutability is opt-in, const is the default. At module level it declares mutable global state. Before & it marks an exclusive reference or receiver instead.
mut temp: TemporaryFilestatic TemporaryFile::create() -> Result<TemporaryFile, IoError>A new, empty file under the temporary directory, open for reading and writing.
std::fs::temporarymut temp: TemporaryFileTemporaryFile::file(mut &this) -> mut &FileThe open file, to read, write and seek.
std::fs::temporaryFile::write<T>(mut &this, data: T) -> Result<(), IoError>via trait Write
Write data in full, retrying short writes and Interrupted errors. Generic over the payload, dispatched at compile time via static match: a raw byte &Slice<u8>, a Str view, a NUL-terminated C string, or a single u8.
type struct SecureRng {}Cryptographically secure source backed by the kernel CSPRNG. Slower than Rng; use it only where unpredictability matters.
TemporaryDirectory::keep(mut this) -> PathBufLeave the directory in place when this value is dropped, and return its path.
std::fs::temporarystatic name(param: Type) -> Ret { ... }A method that belongs to the type rather than an instance, called as Type::name(..). For structs, static new(..) returning a struct literal is the idiomatic constructor.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}The error of a failed I/O operation: its kind, the operating system's own code, and, when the operation named one, what it was doing and the path. It prints as could not open config.toml: not found.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type struct Path {}A borrowed filesystem path: a view of text owned elsewhere, valid while that text is. A string literal or a Str converts to one where a Path is wanted; PathBuf is the owned form join builds.
mut thisA by-value receiver the body may also reassign. The receiver is moved into the method and consumed; the caller's value may not be used afterwards.
language referencemut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement trait Trait for Type { ... }Introduces a trait implementation: the block supplies, for one concrete type, the method bodies the trait requires.
language referencetype trait Drop {
drop(mut &this) -> void;
}A type that releases something when a value of it ends: memory, a file handle, a lock. The compiler calls drop once when an owned value goes out of scope; release a value early with mem::drop(value).
implement trait Trait for Type { ... }Names the type a trait implementation is for. The block that follows supplies the bodies the trait requires.
language reference