2

The Rust code is

use std::process::Command;

fn main() {
    if cfg!(target_os = "windows") {
        Command::new("cls").spawn().expect("cls command failed to start");
    } else {
        println!("Not windows");
    }
}

and the error is

PS C:\dev\Rust\testing> cargo run
   Compiling testing v0.1.0 (C:\dev\Rust\testing)
    Finished dev [unoptimized + debuginfo] target(s) in 0.95s
     Running `target\debug\testing.exe`
thread 'main' panicked at 'cls command failed to start: Os { code: 2, kind: NotFound, message: "The system cannot find the file 
specified." }', src\main.rs:5:37
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
error: process didn't exit successfully: `target\debug\testing.exe` (exit code: 101)

with RUST_BACKTRACE="full":

PS C:\dev\Rust\testing> cargo run
   Compiling testing v0.1.0 (C:\dev\Rust\testing)
    Finished dev [unoptimized + debuginfo] target(s) in 1.12s
     Running `target\debug\testing.exe`
thread 'main' panicked at 'cls command failed to start: Os { code: 2, kind: NotFound, message: "The system cannot find the file specified." }', src\main.rs:5:37
stack backtrace:
   0:     0x7ff7c9ecf059 - std::backtrace_rs::backtrace::dbghelp::trace
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs:98
   1:     0x7ff7c9ecf059 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66
   2:     0x7ff7c9ecf059 - std::sys_common::backtrace::_print_fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:79
   3:     0x7ff7c9ecf059 - std::sys_common::backtrace::_print::{{impl}}::fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:58
   4:     0x7ff7c9ee32fb - core::fmt::write
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\fmt\mod.rs:1080
   5:     0x7ff7c9ecc598 - std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\io\mod.rs:1516
   6:     0x7ff7c9ed1ae4 - std::sys_common::backtrace::_print
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:61
   7:     0x7ff7c9ed1ae4 - std::sys_common::backtrace::print
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:48
   8:     0x7ff7c9ed1ae4 - std::panicking::default_hook::{{closure}}
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:208
   9:     0x7ff7c9ed16c8 - std::panicking::default_hook
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:227
  10:     0x7ff7c9ed239f - std::panicking::rust_panic_with_hook
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:577
  11:     0x7ff7c9ed1f05 - std::panicking::begin_panic_handler::{{closure}}
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:484
  12:     0x7ff7c9ecf93f - std::sys_common::backtrace::__rust_end_short_backtrace<closure-0,!>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:153
  13:     0x7ff7c9ed1eb9 - std::panicking::begin_panic_handler
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:483
  14:     0x7ff7c9ee2590 - core::panicking::panic_fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\panicking.rs:85
  15:     0x7ff7c9ee23b3 - core::option::expect_none_failed
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\option.rs:1234
  16:     0x7ff7c9ec11c8 - core::result::Result<std::process::Child, std::io::error::Error>::expect<std::process::Child,std::io::error::Error>
                               at C:\Users\sadic\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\result.rs:933
  17:     0x7ff7c9ec5f8f - testing::main
                               at C:\dev\Rust\testing\src\main.rs:5
  18:     0x7ff7c9ec31eb - core::ops::function::FnOnce::call_once<fn(),tuple<>>
                               at C:\Users\sadic\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
  19:     0x7ff7c9ec1d7b - std::sys_common::backtrace::__rust_begin_short_backtrace<fn(),tuple<>>
                               at C:\Users\sadic\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\sys_common\backtrace.rs:137
  20:     0x7ff7c9ec40e1 - std::rt::lang_start::{{closure}}<tuple<>>
                               at C:\Users\sadic\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:66
  21:     0x7ff7c9ed25a3 - core::ops::function::impls::{{impl}}::call_once
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops\function.rs:280
  22:     0x7ff7c9ed25a3 - std::panicking::try::do_call
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:381
  23:     0x7ff7c9ed25a3 - std::panicking::try
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:345
  24:     0x7ff7c9ed25a3 - std::panic::catch_unwind
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panic.rs:382
  25:     0x7ff7c9ed25a3 - std::rt::lang_start_internal
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\rt.rs:51
  26:     0x7ff7c9ec40b3 - std::rt::lang_start<tuple<>>
                               at C:\Users\sadic\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:65
  27:     0x7ff7c9ec6000 - main
  28:     0x7ff7c9ee76c8 - invoke_main
                               at d:\agent\_work\63\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  29:     0x7ff7c9ee76c8 - __scrt_common_main_seh
                               at d:\agent\_work\63\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  30:     0x7ffef5167034 - BaseThreadInitThunk
  31:     0x7ffef617d0d1 - RtlUserThreadStart
error: process didn't exit successfully: `target\debug\testing.exe` (exit code: 101)

in contrast I have a C++ source file doing exactly the same successfully

#include <stdlib.h>

int main() {
    system("cls");
}

I installed rustup using rustup_init.exe from rustup.rs and I am using cargo nightly to run the program, the stable branch does the same thing, I also have installed mingw64 and llvm if that helps. I didn't found anyone with the same problem in the internet. So, what it's happening and what do I do to solve this?

vallentin
  • 23,478
  • 6
  • 59
  • 81

1 Answers1

5

The reason it doesn't work is because cls is not an executable on Windows, it's a command of the Windows command line interpreter.

So if you want to execute it, you instead have to execute cmd /c cls.

Command::new("cmd")
    .args(&["/c", "cls"])
    .spawn()
    .expect("cls command failed to start")
    .wait()
    .expect("failed to wait");

Alternatively, if you just want to clear the screen, you could also use the console crate.

// console = "0.14.0"
let term = console::Term::stdout();
term.clear_screen().expect("failed clearing screen");

Related:

While those questions are not related to Rust, they do elaborate on cls not being an executable.

vallentin
  • 23,478
  • 6
  • 59
  • 81