From 7a22d5567a3f1b96e4dedff025ca57260927fc57 Mon Sep 17 00:00:00 2001 From: Andrew Walbran Date: Wed, 15 Mar 2023 08:06:35 +0000 Subject: [PATCH] Don't allocate in success case (#506) `format!` was being called (and allocating a string) even in the success case. --- src/error-handling/error-contexts.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/error-handling/error-contexts.md b/src/error-handling/error-contexts.md index bb676d39..50fb5948 100644 --- a/src/error-handling/error-contexts.md +++ b/src/error-handling/error-contexts.md @@ -12,7 +12,7 @@ use anyhow::{Context, Result, bail}; fn read_username(path: &str) -> Result { let mut username = String::with_capacity(100); fs::File::open(path) - .context(format!("Failed to open {path}"))? + .with_context(|| format!("Failed to open {path}"))? .read_to_string(&mut username) .context("Failed to read")?; if username.is_empty() {