No println through rustc on M1

Hi, I am new to rust
running stable 1.55.0 on M1 Mac

fn main() {
    println!("Hello, World!");
}

returns nothin through:

rustc tests/hello-world.rs -A warnings

With rustc you’re only compiling your program, not running it. You’ll still need to execute the executable file it produces, e.g. try running
./tests/hello_world
./hello_world

Assuming you’re having a standard rust installation, you should have the tool cargo available. Typically rustc is never used manually, instead we interact with it through cargo.

  • create a new project with
    cargo new name_of_my_project
  • navigate into the newly created project directory
    cd name_of_my_project
  • run the code with
    cargo run

Read more in the book

Hello, Cargo! - The Rust Programming Language

2 Likes

That works thanks!
but is very impractical for practice or testing, any idea why rustc doesn't work?
do you know any other way to run code snippets?

As I said:

I.e. after

rustc tests/hello-world.rs 

also run

./test/hello_world
./hello_world

(or maybe ./hello_world, I’m not 100% sure where it will place the executable)

Edit: Turns out, it goes into the current directory.

I find it very practical for practice and testing.

Consider that:

  1. One can run the code being developed with:
cargo run

which provides a debug build. Or one can get an optimised release build with:

cargo run --release

This is convenient because it will automatically fetch and build any crate dependencies you are using. As listed in the Cargo.toml file.

  1. While hacking on code one often wants to just get the damn thing to compile. For that there is:
cargo check

Which will check out syntax and the like but does not waste time building the actual program.

  1. You can add tests to the module you are developing and run them with:
cargo test

See: How to Write Tests - The Rust Programming Language

  1. You can write benchmarks and run them with:
cargo bench

See: cargo bench - The Cargo Book

I'm doing all this just now as I write a module to implement some new functionality into a much bigger project. I develop the functionality in a little stand alone project all kicked off with 'cargo new'

3 Likes

If you just want to run snippets, there's always the Playground.

1 Like

Thanks a lot
so I made a function in .zshrc:

rustx() {
  rustc -C target-cpu=native $1.rs && ./$1 
}

so now I don't need to set up anything :slight_smile:

Consider adding --edition=2018 (or, soon, --edition=2021) to that invocation. Also note that you won't be able to take advantage of any crates besides std with this approach, that's what Cargo is for.

2 Likes

Direct use of rustc is typically for advanced uses only. You should use cargo run instead, especially if you're new to Rust and you're not building a custom complex build pipeline for non-standard programs.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.