“borrowed value does not live long enough” seems to blame the wrong thing

后端 未结 2 1404
生来不讨喜
生来不讨喜 2020-11-27 08:13

I am counting the number of times a word appears in Macbeth:

use std::io::{BufRead, BufReader};
use std::fs::File;
use std::collections::HashMap;

fn main()          


        
2条回答
  •  执念已碎
    2020-11-27 08:41

    The error is both right and wrong here. l is blamed, because w lives only as long as l (and l.unwrap()) and l doesn't live long enough to put it in hashmap in a higher scope.

    In practice, you just have to look at what other variables depend on the lifetime of a variable the compiler complains about.

    But Rust is also working lately on improving error reporting, so I'd raise this case as potential bug.

提交回复
热议问题