Show how to build and execute a binary, how to build a crate, and how to build and run a test.
In this example we have declared 3 targets. A library crate, an executable, and a test. The binary and the test depends on the library crate.
-
//:my_lib: A library crate with a single rs file containing unit tests in the same file. It can be defined in Bazel as follows:rust_library( name = "my_lib", srcs = ["my_lib.rs"], )
nameis the name for the target, it will also be used to create the library file in the form oflib<name>-<hash>.rlib.srcsis the list of source files needed to generate the create. If more than one file is provided, at least one must be calledlib.rsor the parametercrate_rootmust be provided specifying the root of the crate. -
//:hello_world: A binary that depends on the library mentioned above. It can be defined in Bazel as follows:rust_binary( name = "hello_world", srcs = ["hello_world.rs"], deps = [":my_lib"], )
nameis the name for the target, it will also be used to create the executable file in the form of<name>in Unix systems or<name>.exein Windows systems.srcsis the list of source files needed to generate the executable.depsis the list of libraries that your binary depends on. In this case we specify the target:my_libthat is the library that we want to use in this binary. -
//:my_test: A test for target for the tests inside themy_libtarget. In the end it is an executable also but Bazel will treat it different in several aspects. It can be defined in Bazel as follows:rust_test( name = "my_test", crate = ":my_lib", )
nameis the name for the target, it will also be used to create the executable file for the test in the form of<name>in Unix systems or<name>.exein Windows systems.crateis the name of the target crate that contains the tests. Typically the crate being tested.
To run the executable you call:
bazel run //:hello_worldIt will run the executable and display the output on the terminal.
To run the test you call:
bazel test //:my_testIt will run the test without showing any output from the test but a Bazel output indicating if the test succeeded or failed.
//:my_test PASSED in 0.0sA test, compared to a binary executed with bazel run will not be allways executed. If Bazel detects that nothing changed it will cache the result of th test and show it in the report.
//:my_test (cached) PASSED in 0.0sIf you want to build the executable without running it you call:
bazel build //:hello_worldAnd if you want to build only the library you can do the same with the target of the library:
bazel build //:my_lib