Std misc Many other types are provided by the std library to support things such as: • Threads • Channels • File I/O These expand beyond what the primitives provide. See also: primitives and the std library
Threads Rust provides a mechanism for spawning native OS threads via the spawn function, the argument of this function is a moving closure. use std::thread; const NTHREADS: u32 = 10; // This is the `main` thread fn main() { // Make a vector...
Testcase: map-reduce Rust makes it very easy to parallelize data processing, without many of the headaches traditionally associated with such an attempt. The standard library provides great threading primitives out of the box. These, combin...
Channels Rust provides asynchronous channels for communication between threads. Channels allow a unidirectional flow of information between two end-points: the Sender and the Receiver. use std::sync::mpsc::{Sender, Receiver}; use std::sync:...
Path The Path type represents file paths in the underlying filesystem. Across all platforms there is a single std::path::Path that abstracts over platform-specific path semantics and separators. Bring it into scope with use std::path::Path;...
File I/O The File struct represents a file that has been opened (it wraps a file descriptor), and gives read and/or write access to the underlying file. Since many things can go wrong when doing file I/O, all the File methods return the io:...
open The open function can be used to open a file in read-only mode. A File owns a resource, the file descriptor and takes care of closing the file when it is droped. use std::fs::File; use std::io::prelude::*; use std::path::Path; fn main(...
create The create function opens a file in write-only mode. If the file already existed, the old content is destroyed. Otherwise, a new file is created. static LOREM_IPSUM: &str = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, s...
read_lines A naive approach This might be a reasonable first attempt for a beginner's first implementation for reading lines from a file. use std::fs::read_to_string; fn read_lines(filename: &str) -> Vec<String> { let mut result = Vec::new(...
Child processes The process::Output struct represents the output of a finished child process, and the process::Command struct is a process builder. use std::process::Command; fn main() { let output = Command::new("rustc") .arg("--version")...
Pipes The std::process::Child struct represents a child process, and exposes the stdin, stdout and stderr handles for interaction with the underlying process via pipes. use std::io::prelude::*; use std::process::{Command, Stdio}; static PAN...
Wait If you'd like to wait for a process::Child to finish, you must call Child::wait, which will return a process::ExitStatus. use std::process::Command; fn main() { let mut child = Command::new("sleep").arg("5").spawn().unwrap(); let _resu...
Filesystem Operations The std::fs module contains several functions that deal with the filesystem. use std::fs; use std::fs::{File, OpenOptions}; use std::io; use std::io::prelude::*; #[cfg(target_family = "unix")] use std::os::unix; #[cfg(...
Program arguments Standard Library The command line arguments can be accessed using std::env::args, which returns an iterator that yields a String for each argument: use std::env; fn main() { let args: Vec<String> = env::args().collect(); /...
Argument parsing Matching can be used to parse simple arguments: use std::env; fn increase(number: i32) { println!("{}", number + 1); } fn decrease(number: i32) { println!("{}", number - 1); } fn help() { println!("usage: match_args <string...
Foreign Function Interface Rust provides a Foreign Function Interface (FFI) to C libraries. Foreign functions must be declared inside an extern block annotated with a #[link] attribute containing the name of the foreign library. use std::fm...
Testing Rust is a programming language that cares a lot about correctness and it includes support for writing software tests within the language itself. Testing comes in three styles: • Unit testing. • Doc testing. • Integration testing. Al...
Unit testing Tests are Rust functions that verify that the non-test code is functioning in the expected manner. The bodies of test functions typically perform some setup, run the code we want to test, then assert whether the results are wha...
Documentation testing The primary way of documenting a Rust project is through annotating the source code. Documentation comments are written in CommonMark Markdown specification and support code blocks in them. Rust takes care about correc...
Integration testing Unit tests are testing one module in isolation at a time: they're small and can test private code. Integration tests are external to your crate and use only its public interface in the same way any other code would. Thei...