I am using the Diesel ORM wrapper with PostgreSQL. I was following the guide on their website which has the following code:
pub fn establish_connection() -> PgConnection {
dotenv().ok();
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.expect(&format!("Error connecting to {}", database_url))
}
I understood what dotenv()
does through the dotenv docs — it loads the env file. In the source code I saw that that dotenv()
returns a Result
. What does ok()
do then? Does it unwrap the result? If so, why not use unwrap()
?
unwrap
canpanic
, butok
can't, so the caller can handle the failure case – Pictographok()
is not for handling the failure case, it's for ignoring the failure case. When handling failure, one would not typically callok()
(because it throws away the error information), but match the returnedResult
directly. – Ms