Build Your First Rust Binary Crate
This challenge will guide you through the fundamental process of creating a Rust binary crate. Understanding how to structure and build a simple executable program is the first step towards developing more complex applications and tools in Rust.
Problem Description
Your task is to create a new Rust binary crate named hello_rust. This crate should contain a single executable file that, when run, prints the message "Hello, Rust!" to the console. You will need to use Cargo, Rust's build system and package manager, to create, build, and run your project.
Key Requirements:
- Create a new Rust binary project using Cargo.
- The project should be named
hello_rust. - The executable, when run, must output the exact string "Hello, Rust!" followed by a newline.
- The project should be buildable and runnable using standard Cargo commands.
Expected Behavior:
When you navigate to the project directory and run cargo run, the program should execute and display:
Hello, Rust!
Examples
Example 1:
- Action: Create a new binary project and run it.
- Command:
cargo new hello_rust --bin cd hello_rust cargo run - Output:
Hello, Rust! - Explanation: The
cargo newcommand creates the basic project structure. The defaultmain.rsfile generated bycargo new --binusually contains aprintln!("Hello, world!");statement. You will modify this to print "Hello, Rust!".cargo runthen compiles and executes the program.
Example 2:
- Action: After modifying
src/main.rsto print the correct message, build the project in release mode and then run the executable directly. - Command:
cargo build --release ./target/release/hello_rust - Output:
Hello, Rust! - Explanation:
cargo build --releasecompiles the code with optimizations. The executable is then located in./target/release/. Running this executable directly produces the desired output.
Constraints
- The project must be created using Cargo.
- The crate name must be
hello_rust. - The output message must be exactly "Hello, Rust!" (case-sensitive, with a newline).
Notes
- Cargo is your primary tool for this challenge. Familiarize yourself with
cargo new,cargo run, andcargo build. - The primary logic will reside in the
src/main.rsfile. - Pay close attention to the exact wording and capitalization of the output message.
- This challenge is designed to introduce you to the basic workflow of Rust development with Cargo.