Skip to content

Latest commit

 

History

History
67 lines (43 loc) · 1.49 KB

std-lib-tour.md

File metadata and controls

67 lines (43 loc) · 1.49 KB

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

std::marker::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

std::process::Command

A process builder, providing fine-grained control over how a new process should be spawned.

Used for interacting with other executables.

use std::process::Command;

fn example() {
    Command::new("ls")
            .args(&["-l", "-a"])
            .spawn()
            .expect("ls command failed to start");
}

Filesystem Manipulation

std::fs & std::path

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);
}