Hone logo
Hone
Problems

Building a Simple Command-Line Greeter in Rust

This challenge guides you through creating a basic binary crate in Rust. You'll build a command-line application that takes a name as an argument and prints a personalized greeting. This exercise reinforces fundamental Rust concepts like crate structure, command-line argument parsing, and basic output.

Problem Description

Your task is to create a Rust binary crate named greeter that accepts a name as a command-line argument and prints a greeting message. The program should:

  1. Parse the command-line argument: The program should accept a single argument representing the user's name. If no argument is provided, it should use a default name ("World").
  2. Construct the greeting message: The program should create a string that says "Hello, [name]!".
  3. Print the greeting: The program should print the constructed greeting message to the standard output.
  4. Handle errors gracefully: If there's an error parsing the command-line arguments, the program should print an error message to standard error and exit with a non-zero exit code.

Key Requirements:

  • The crate must be a binary crate (not a library).
  • The program must compile and run without warnings.
  • The program must handle the case where no command-line argument is provided.
  • The program must handle potential errors during argument parsing.

Examples

Example 1:

Input: ./target/debug/greeter John
Output: Hello, John!

Explanation: The program receives "John" as the command-line argument and constructs the greeting "Hello, John!".

Example 2:

Input: ./target/debug/greeter
Output: Hello, World!

Explanation: No command-line argument is provided, so the program uses the default name "World" and constructs the greeting "Hello, World!".

Example 3: (Error Handling)

Input: ./target/debug/greeter John Doe
Output: Error: Expected exactly one argument.

Explanation: The program receives two arguments, which is an error. It prints an error message to standard error.

Constraints

  • The program must be written in Rust.
  • The program should use the standard library for argument parsing.
  • The program should compile and run on a standard Linux/macOS/Windows environment with a Rust toolchain installed.
  • The program should be reasonably efficient (no unnecessary allocations or computations). Performance is not a primary concern, but avoid obviously inefficient code.

Notes

  • Consider using the std::env module for accessing command-line arguments.
  • The clap crate is a popular choice for more complex argument parsing, but for this simple challenge, using std::env is sufficient.
  • Think about how to handle the case where the provided name contains spaces or other special characters. For this challenge, you can assume the name will be a single word.
  • Focus on creating a clean and readable solution. Good error handling is important.
  • Remember to create a Cargo.toml file to define your crate. The [dependencies] section should be empty for this simple example.
Loading editor...
rust