Lines>::count() takes ownership

⚓ rust    📅 2025-06-09    👤 surdeus    👁️ 2      

surdeus

Hi there!

I have following issue, where the count() method for some reasons takes ownership of the object, and returns nothing but size. According to this issue, I can't use this object in the iteration below:

let lines = BufReader::new(File::open(&argument.source)?).lines();

let mut index: HashMap<String, usize> = 
    HashMap::with_capacity(lines.count()); // `count` takes `lines` ownership

'l: for line in lines { // the `lines` moved (error)
  • I want to count the lines, to HashMap with optimal capacity, that based on this value.

3 posts - 2 participants

Read full topic

🏷️ rust_feed