std
Library Tour
It's time for a tour of some interesting parts in std
.
We will focus on parts we have not otherwise covered.
PhantomData
Zero-sized types are used to mark things that "act like" they own a T
.
These are useful for types which require markers, generics, or use unsafe code.
use std::marker::PhantomData; struct HttpRequest<ResponseValue> { // Eventually returns this type. response_value: PhantomData<ResponseValue>, } fn main() {}
Command
A process builder, providing fine-grained control over how a new process should be spawned.
Used for interacting with other executables.
#![allow(unused)] fn main() { use std::process::Command; fn example() { Command::new("ls") .args(&["-l", "-a"]) .spawn() .expect("ls command failed to start"); } }
Filesystem Manipulation
Path handling and file manipulation.
use std::fs::{File, canonicalize}; use std::io::Write; fn main() { let mut file = File::create("foo.txt").unwrap(); file.write_all(b"Hello, world!").unwrap(); let path = canonicalize("foo.txt").unwrap(); let components: Vec<_> = path.components().collect(); println!("{:?}", components); }