Std::process::exit() behaves differently Linux vs Windows


#1

Here is a tiny program:

use std::env;
use std::process;

fn main() {
    let code: i32 = env::args().skip(1).next().unwrap().parse().unwrap();
    println!("Got {}", code);
    process::exit(code);
}

Here are two runs on Linux:

: crr.sh 0
    Finished release [optimized] target(s) in 0.0 secs
     Running `target/release/app 0`
Got 0
: crr.sh 1
    Finished release [optimized] target(s) in 0.0 secs
     Running `target/release/app 1`
Got 1

(crr.sh & crr.bat just do cargo run --release)
But on Windows it is different:

V:\tmp\app>cargo run --release -- 0
    Finished release [optimized] target(s) in 0.11 secs
     Running `target\release\app.exe 0`
Got 0
V:\tmp\app>crr 1

V:\tmp\app>cargo run --release -- 1
    Finished release [optimized] target(s) in 0.10 secs
     Running `target\release\app.exe 1`
Got 1
error: process didn't exit successfully: `target\release\app.exe 1` (exit code: 1)

I am trying to write a command line program on Windows and ideally I’d like to be able to return an int to the OS so that people can use it as %errorlevel% in .bat files without the extra error message. Just a nice to have though.


#2

Instead of cargo run, run the actual exe from ./target/release.


#3

You are quite right! Sorry for my mistake:-(
Thanks.

(In fact, that’s actually a useful feature since it makes it easy to check that you’re getting the expected return code on Windows:-)


#4

But behaviour is still different :slight_smile: is it expected?


#5

It was my mistake. The executable’s behavior is the same for Linux & Windows: the difference is that cargo reports non-zero return value on Windows but not on Linux (which for me is useful).