Hello!,

I'm learning rust and finding myself fighting the language a little and so
I could do with a bit of help.

In my code completion project I have a function which parses 'use' view
items (using libsyntax) and currently returns a vector of vectors of
strings representing fully qualified paths (e.g. std::foo::bar).
I also have a function which traverses crates and resolves a path to an
item source position. It takes the path as a reference to a slice of string
references. Here's a stubbed out illustration of the code:

pub fn parse_view_item() -> Vec<Vec<~str>> {
    // stub code:
    let mut vv = Vec::new();
    let mut v = Vec::new();
    v.push(~"std");
    v.push(~"foo");
    v.push(~"bar");
    vv.push(v);
    return vv
}

pub fn search_crate(path : &[&str]) {
    // ...
}

fn main() {
    let paths = parse_view_item();

    for path in paths.iter() {
        // translate Vec<~str> to Vec<&str>
        let mut v2 = Vec::new();
        for item in path.iter() {
            v2.push(item.as_slice());
        }

        search_crate(v2.as_slice());
    }
}

The issue is that parse_view_item returns owned strings and search_crate()
wants borrowed string references, so I end up unpacking each path into a
new vector.

How should I restructure this code to make things a bit nicer?
 - can parse_view_item return a narrower interface?
 - is there a way to avoid the re-packing?
 - can I do something with iterators to make this nicer?  (so far I've
favoured using slices over iterators because it makes fn signatures easier
on the eye)
 - will the upcoming string changes make this better?

Any pointers much appreciated!

Cheers,

Phil
_______________________________________________
Rust-dev mailing list
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to