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
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.
cargo new name_of_my_project
cd name_of_my_project
cargo run
Read more in the book
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:
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.
cargo check
Which will check out syntax and the like but does not waste time building the actual program.
cargo test
See: How to Write Tests - The Rust Programming Language
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'
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
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.
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.
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.