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...
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...
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...
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(); /...
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(...
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...
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...
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")...
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(...
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...
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(...
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:...
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;...
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:...
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...
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...
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
Arc When shared ownership between threads is needed, Arc(Atomically Reference Counted) can be used. This struct, via the Clone implementation can create a reference pointer for the location of a value in the memory heap while increasing the...
Rc When multiple ownership is needed, Rc(Reference Counting) can be used. Rc keeps track of the number of the references which means the number of owners of the value wrapped inside an Rc. Reference count of an Rc increases by 1 whenever an...
HashSet Consider a HashSet as a HashMap where we just care about the keys ( HashSet<T> is, in actuality, just a wrapper around HashMap<T, ()>). "What's the point of that?" you ask. "I could just store the keys in a Vec." A HashSet's unique...