Cannot use moved BufReader after for loop with bufreader.lines()

后端 未结 2 1551
孤独总比滥情好
孤独总比滥情好 2020-12-06 19:24

I\'m trying to read some lines from a file, skipping the first few and printing the rest, but I keep getting errors about used value after move:

use std::fs:         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-06 19:38

    In order to avoid the move, use the Read::by_ref() method. That way, you only borrow the BufReader:

    for (index, line) in buffer.by_ref().lines().enumerate() { ... }
    //                         ^^^^^^^^^
    // you can still use `buffer` here
    

提交回复
热议问题