set
Sets key to value in dotenv content and returns the new content.
The first existing assignment of key is replaced in place and any later ones are dropped;
when there is none, a new line is appended. Every other line (comments, blank lines, other
variables) is left untouched, and the replaced line keeps its line ending. value is quoted
and escaped only when needed, so get reads it back exactly.
Import
Section titled “Import”Cargo feature env (enabled by default). To compile only this module:
or in Cargo.toml:
Signature
Section titled “Signature”Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
content | &str | The dotenv text to edit. |
key | &str | The variable name to set. |
value | &str | The value to assign to key. |
Returns
Section titled “Returns”Result<String, InvalidKeyError> — Ok on success, otherwise an Err: see Errors.
Errors
Section titled “Errors”Returns InvalidKeyError when key is not [A-Za-z_][A-Za-z0-9_]*.
Examples
Section titled “Examples”Error type: InvalidKeyError
Section titled “Error type: InvalidKeyError”The variable name passed to set is not a valid name ([A-Za-z_][A-Za-z0-9_]*).
InvalidKeyError::key
Section titled “InvalidKeyError::key”The rejected name.
Returns
&str — The rejected variable name.
