How do I run only a specific file other than main.rs?

I would like to create a new file in the current project just to test this example. The workspace has the default structure after cargo new project. I just want to test this somewhere other than in main.rs. Because I already have code there. How can I do this with cargo? I know I can do rustc test.rs to execute and rustc --test test.rs to test it.

cargo test will run every function with a #[test] attribute, and every main in the tests folder. These are unit and integration tests, respectively.

Learn more here: Tests - The Cargo Book

If you want to have a second program instead of using the test runner, you can put the code in src/bin/example.rs and then run it with cargo run --bin example.

2 Likes

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.