Tuesday, 26 May 2015

Rust: print the program arguments

A simple example using Cargo in Rust 1.0 on Linux.

Install Rust if you haven't. Create a project:

$ cargo new hello --bin

This will create the files:


Replace the contents of main.rs with:

use std::env;

fn main() {
  for argument in env::args() {
    println!("Hello, {}!", argument);

From the hello directory run:

$ cargo run World Kitty "to my little friend, Al Pacino"
     Running `target/debug/hello World Kitty to my little friend, Al Pacino`
Hello, target/debug/hello!
Hello, World!
Hello, Kitty!
Hello, to my little friend, Al Pacino!

Rust provides the executable as the zeroth argument target/debug/hello. The arguments passed to the application start at index 1.

No comments:

Post a Comment

All comments are moderated