Module std::os

Operating system interfaces.

Access command-line arguments, environment variables, and system information.

Examples

import std.os;

fn main() {
    let arguments = os.args();
    match arguments.get(0) {
        .Some(first) => println(first),
        .None => println("no program argument"),
    }
    let home = os.home_dir();
    println(home);
}

Contents

Functions

Function args

pub fn args() -> Vec<string>

Return all command-line arguments, including the program name.

Examples

import std.os;

fn main() {
    let arguments = os.args();
    match arguments.get(0) {
        .Some(program) => println(program),
        .None => println("no program argument"),
    }
}

Function env

pub fn env(name: string) -> Option<string>

Get the value of an environment variable.

Returns None if the variable is not set.

Examples

import std.os;

fn main() {
    let path = os.env("PATH");
}

Function set_env

pub fn set_env(name: string, value: string) -> Result<(), EnvError>

Set an environment variable for the current process.

Environment names must be non-empty and cannot contain =.

Function remove_env

pub fn remove_env(name: string) -> Result<(), EnvError>

Remove an environment variable.

Environment names must be non-empty and cannot contain =.

Function has_env

pub fn has_env(name: string) -> bool

Check whether an environment variable is set.

Function cwd

pub fn cwd() -> string

Returns the current working directory.

Function home_dir

pub fn home_dir() -> string

Returns the user's home directory.

Function hostname

pub fn hostname() -> string

Returns the system hostname.

Function pid

pub fn pid() -> i64

Returns the current process ID.

Function temp_dir

pub fn temp_dir() -> string

Returns the system temporary directory path.

Uses the platform-appropriate temp directory (e.g. /tmp on Unix, %TEMP% on Windows). Trailing separators are stripped.

Examples

import std.os;

fn main() {
    let tmp = os.temp_dir();
    let path = tmp + "/myfile.txt";
}

Types

Enum EnvError

Invalid environment-variable name.

Variants

InvalidName(string)