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_URLis not set, use a default value of"sqlite://db.sqlite". - If
PORTis not set, use a default value of8080. - If
PORTis set but contains a value that cannot be parsed into au16(unsigned 16-bit integer), the program should print an error message and exit gracefully. - The program should print the resolved
DATABASE_URLandPORTto 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
PORTvariable might be set to a non-numeric string or a number outside theu16range.
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
PORTmust be a validu16(0-65535). - The program should handle potential errors during parsing gracefully.
Notes
- Consider using the
std::env::varfunction to read environment variables. - Remember that
std::env::varreturns aResult. You'll need to handle both theOkandErrvariants. - For parsing the
PORTvariable, theparse::<u16>()method on strings will be useful.