Using the "cargo run" Command (with examples)

Using the "cargo run" Command (with examples)

The cargo run command is used to compile and run a Rust package. It is a convenient way to build and execute the default binary target or any specified binary or example from the package.

Running the Default Binary Target

To run the default binary target in your package, simply use the following command:

cargo run

By running the default binary target, you can quickly test and execute your Rust code without explicitly specifying the binary name.

Running a Specified Binary

If you have multiple binaries in your Cargo package, you can specify which one to run using the --bin flag followed by the binary name. Here is an example:

cargo run --bin my_binary

In this example, the cargo run command will compile and run the specific binary named “my_binary”. This is useful when you want to execute a specific part of your code that is encapsulated in a separate binary.

Running a Specified Example

Cargo allows you to include examples within your package, which are separate code snippets demonstrating the usage of your crate. You can run a specific example by utilizing the --example flag followed by the example name. Here is an example:

cargo run --example my_example

Running a specific example is beneficial when you want to test and verify the behavior of a particular functionality showcased within the example.

Activating Features

Cargo provides a feature system that allows conditional compilation and bundling of different parts of your code. You can activate specific features using the --features flag followed by a space or comma-separated list of feature names. Here is an example:

cargo run --features feature1 feature2

By activating specific features, you can selectively compile and run code that relies on those features. This is useful when you want to enable or disable certain optional functionalities.

Disabling Default Features

To disable the default features of your package, you can use the --no-default-features flag. Here is an example:

cargo run --no-default-features

Disabling the default features allows you to exclude the features that are enabled by default when running your code. This is useful when you want to have more control over which features are active.

Activating All Features

If your Cargo package has multiple features, you can activate all of them using the --all-features flag. Here is an example:

cargo run --all-features

Running your code with all features enabled can be useful during development and testing phases when you want to ensure that all functionalities are working as expected.

Running with a Given Profile

Cargo allows you to define different profiles for building and executing your code, such as release or debug profiles. You can specify the profile to use with the --profile flag followed by the profile name. Here is an example:

cargo run --profile release

By running with a specific profile, you can control the optimization levels, enable or disable certain compile-time checks, and affect the overall performance of your code.

Example Outputs

  • Default Binary Target:

    • Motivation: You want to quickly execute and test the main functionality of your Rust package.
    • Explanation: Running cargo run without any additional arguments compiles and runs the default binary target in your Cargo package.
    • Example Output: The program is executed and produces the desired output or performs the expected actions.
  • Specified Binary:

    • Motivation: You have multiple binaries in your package and want to run a specific one to test a particular implementation or use case.
    • Explanation: By using the --bin flag followed by the binary name, you can compile and run the desired binary.
    • Example Output: The specified binary is executed, producing the expected output or performing the desired actions.
  • Specified Example:

    • Motivation: You want to test and verify the behavior of a specific example included in your package.
    • Explanation: By using the --example flag followed by the example name, you can compile and run the desired example.
    • Example Output: The specified example is executed, demonstrating the intended functionality and presenting the expected output.
  • Activating Features:

    • Motivation: Your package has optional features, and you want to selectively enable certain functionalities during execution.
    • Explanation: The --features flag allows you to activate specific features by providing a space or comma-separated list of feature names.
    • Example Output: The code that relies on the activated features is compiled and executed, providing the expected output based on the enabled functionalities.
  • Disabling Default Features:

    • Motivation: You want to exclude the features that are enabled by default and have finer control over the active features during execution.
    • Explanation: Using the --no-default-features flag disables the default features of your package.
    • Example Output: The code is compiled and executed without the features that are enabled by default, resulting in the expected output based on the excluded functionalities.
  • Activating All Features:

    • Motivation: You want to test and ensure that all features of your package work together as expected during development and testing phases.
    • Explanation: By using the --all-features flag, you can activate all available features of your Cargo package.
    • Example Output: The code containing all enabled features is compiled and executed, validating the interactions and producing the expected output.
  • Running with a Given Profile:

    • Motivation: You want to build and run your code with specific optimization levels or enable/disable certain compile-time checks.
    • Explanation: The --profile flag followed by the profile name allows you to specify the profile to use when compiling and executing your code.
    • Example Output: The code is built and executed according to the settings defined in the given profile, affecting the performance and output based on the chosen optimizations and compile-time checks.

Related Posts

How to use the command `dstat` (with examples)

How to use the command `dstat` (with examples)

dstat is a versatile tool for generating system resource statistics. It provides detailed information about CPU, disk, network, paging, and system statistics.

Read More
How to use the command 'phive' (with examples)

How to use the command 'phive' (with examples)

The command ‘phive’ is the Phar Installation and Verification Environment for secure PHP application deployment.

Read More
How to use the command "nm-online" (with examples)

How to use the command "nm-online" (with examples)

This article will demonstrate various use cases of the command “nm-online”, which is used to ask NetworkManager whether the network is connected.

Read More