Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

VS code doesn't give the type for the 'count' variable #42

Closed
jrmuizel opened this issue Apr 3, 2017 · 4 comments
Closed

VS code doesn't give the type for the 'count' variable #42

jrmuizel opened this issue Apr 3, 2017 · 4 comments

Comments

@jrmuizel
Copy link

jrmuizel commented Apr 3, 2017

In the following code RLS can't seem to figure out the type of 'count'. It figures out line just fine.

use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;

use std::collections::BTreeMap;
use std::collections::btree_map::Entry::*;
use std::env;

fn main() {
    let arg = env::args().nth(1).unwrap();
    let f = File::open(arg).unwrap();
    let mut uniq = BTreeMap::new();
    for l in BufReader::new(&f).lines() {
        let l = l.unwrap();
        match uniq.entry(l) {
            Vacant(v) => { v.insert(1); }
            Occupied(mut o) => { let r = o.get_mut(); *r = *r + 1; }
        }
    }

    for (line, count) in uniq.iter() {
        println!("{} {}", line, count);
    }

}
@sophiajt
Copy link

sophiajt commented Apr 3, 2017

If you hover count in the body, it does work, but not in the destructure.

Also noticed that .lines() doesn't get a hover.

@jrmuizel
Copy link
Author

jrmuizel commented Apr 3, 2017

It gives me a type of 'fn () -> ()' for count in the body which is wrong. It should be &i32.

@sophiajt
Copy link

sophiajt commented Apr 4, 2017

Playing with this example, it seems that count is missing or wrong, but if you comment out any of the lines before it, the type is correctly guessed:

use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;

use std::collections::BTreeMap;
use std::collections::btree_map::Entry::*;
use std::env;

fn main() {
    let arg = env::args().nth(1).unwrap();
    let f = File::open(arg).unwrap();
    let mut uniq: BTreeMap<String, u32> = BTreeMap::new();
    for l in BufReader::new(&f).lines() {
        let l = l.unwrap();
        match uniq.entry(l) {
            Vacant(v) => { /*v.insert(1);*/ }
            Occupied(mut o) => { let r = o.get_mut(); *r = *r + 1; }
        }
    }

    let (line, count) = uniq.iter().next().unwrap();
}

If you change Vacant(v) to Vacant(_) it seems to correctly guess the type.

@nrc
Copy link
Member

nrc commented Aug 7, 2017

@nrc nrc closed this as completed Oct 30, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants