Rust

This article provides some code samples in Rust.

Command line interface

Command arguments

cargo run foo bar baz "This string is a sentence"
use std::env;

// cargo run hello world

fn main() {
    let args: Vec<String> = env::args().collect();

    // Retrieve first argument
    let input = args[1].clone();

    // Print all arguments
    println!("{:?}", args); // ["target/debug/executable", "foo", "bar", "baz", "This string is a sentence"]
}

Standard input

use std::io;

fn main() {
    // empty string
    let mut input = String::new();

    // Collecting user input
    println!("Enter your name: ");
    io::stdin()
        .read_line(&mut input)
        .expect("Failed to read line");

    println!("Received name: {}", input);
}