mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-07 18:16:15 +02:00
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.
This commit is contained in:
parent
c8f626e573
commit
0a9dd33e58
@ -31,7 +31,7 @@ fn log<W: Write>(writer: &mut W, msg: &str) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let mut buffer = Vec::with_capacity(1024);
|
let mut buffer = Vec::new();
|
||||||
log(&mut buffer, "Hello")?;
|
log(&mut buffer, "Hello")?;
|
||||||
log(&mut buffer, "World")?;
|
log(&mut buffer, "World")?;
|
||||||
println!("Logged: {:?}", buffer);
|
println!("Logged: {:?}", buffer);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user