SultanC
  1. Home
  2. Examples
  3. Input and output

Reading a line from standard input

Try: echo Noor | ./program

io/read_input.sn
// Reading a line from standard input. Try: echo Noor | ./program
import std.io.println;
import std.io.read_line;
import std.text.transform;

def main(): int {
    match read_line() with
    | result.ok(line) -> {
        let name: string = trim(&line);
        if (length(name) == 0) {
            println("no input");
        } else {
            let parts: vector string = ["Hello, ", name];
            let nothing: string = "";
            println(join(&parts, &nothing));
        };
    }
    | result.err(_) -> { println("no input"); };
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/io/read_input.sn

Or compile it to an executable:

./build/sultanc examples/en/io/read_input.sn -o build/example && ./build/example

Source on GitHub   All examples