Are there any caveats to unspecified type?

⚓ rust    📅 2025-05-20    👤 surdeus    👁️ 4      

surdeus

Warning

This post was published 40 days ago. The information described in this article may have changed.
fn main() {
  let mut v = Vec::new();
  let s = String::from("Hello ");
  v.push(s);
  v[0].push_str("world");
  println!("original: {}", s);// comment and compiles
  println!("new: {}", v[0]);
}

In this snippet from book, s is moved on push. But I thought this code wouldn't compile because Vec does not have a specified type, and isn't inferred from ::new().

So the compiler first accepts it can be anything, and then fills up that info? Is this a safe pattern to use?

6 posts - 6 participants

Read full topic

🏷️ rust_feed