From 0a9dd33e584f40b61b267e8e425e4a938f42e6d9 Mon Sep 17 00:00:00 2001 From: Marshall Pierce <575695+marshallpierce@users.noreply.github.com> Date: Fri, 6 Jan 2023 09:47:42 -0700 Subject: [PATCH] Create an empty rather than pre-allocating It might give the impression that you can only write to a Vec that has capacity, when in fact Vec's Write impl will grow the storage as needed. While pre-allocating is probably a good efficiency win in many circumstances, I think it's probably worth minimizing the number of concepts in play in this example. --- src/traits/read-write.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/traits/read-write.md b/src/traits/read-write.md index 59303408..c363fc36 100644 --- a/src/traits/read-write.md +++ b/src/traits/read-write.md @@ -31,7 +31,7 @@ fn log(writer: &mut W, msg: &str) -> Result<()> { } fn main() -> Result<()> { - let mut buffer = Vec::with_capacity(1024); + let mut buffer = Vec::new(); log(&mut buffer, "Hello")?; log(&mut buffer, "World")?; println!("Logged: {:?}", buffer);