Hone logo
Hone
Problems

Reading Environment Variables in Rust

Applications often need to be configured externally, and environment variables are a common and flexible way to achieve this. This challenge will test your ability to read and process environment variables in a Rust application, ensuring it can adapt to different configurations without code changes.

Problem Description

Your task is to write a Rust program that reads specific environment variables and uses their values to configure its behavior. You will need to handle cases where variables are present, absent, or contain invalid data, providing sensible defaults or appropriate error messages.

Key Requirements:

  • Read an environment variable named DATABASE_URL. This variable should contain a connection string for a database.
  • Read an environment variable named PORT. This variable should specify the network port the application should listen on.
  • If DATABASE_URL is not set, use a default value of "sqlite://db.sqlite".
  • If PORT is not set, use a default value of 8080.
  • If PORT is set but contains a value that cannot be parsed into a u16 (unsigned 16-bit integer), the program should print an error message and exit gracefully.
  • The program should print the resolved DATABASE_URL and PORT to standard output.

Expected Behavior:

The program should output the final, resolved DATABASE_URL and PORT values.

Edge Cases to Consider:

  • Environment variables are case-sensitive.
  • The PORT variable might be set to a non-numeric string or a number outside the u16 range.

Examples

Example 1:

# No environment variables set
cargo run
DATABASE_URL: sqlite://db.sqlite
PORT: 8080

Explanation: Both environment variables are absent, so the default values are used.

Example 2:

# Custom environment variables set
export DATABASE_URL="postgresql://user:pass@host:port/dbname"
export PORT="3000"
cargo run
DATABASE_URL: postgresql://user:pass@host:port/dbname
PORT: 3000

Explanation: Both environment variables are set to valid values, which are then used.

Example 3:

# Invalid PORT value
export PORT="invalid_port"
cargo run
Error: Invalid PORT value: invalid_port. Please provide a valid u16 number.

Explanation: The PORT environment variable contains a non-numeric string, triggering an error message. The program should exit without printing a resolved port.

Example 4:

# PORT value out of range
export PORT="70000"
cargo run
Error: Invalid PORT value: 70000. Please provide a valid u16 number.

Explanation: The PORT environment variable contains a number that is too large for a u16, triggering an error message.

Constraints

  • The program must be written in Rust.
  • You should use the standard library for reading environment variables.
  • The PORT must be a valid u16 (0-65535).
  • The program should handle potential errors during parsing gracefully.

Notes

  • Consider using the std::env::var function to read environment variables.
  • Remember that std::env::var returns a Result. You'll need to handle both the Ok and Err variants.
  • For parsing the PORT variable, the parse::<u16>() method on strings will be useful.
Loading editor...
rust