0
votes

Here is the files that I have

main.rs

mod sort;

fn main() {
    let v = ~[0,1,3,7,5,7,9,4,2,6,9,5,3,5,0,7,6,9,0,2,3,4,2,4,7,9,7];
    sort::mergeSort(v);
    sort::print(v);
}

and

sort.rs

use std::vec;
pub fn mergeSort<T: Clone+Ord>(a: &mut[T]){
    let mid = a.len()/2 as uint;
    let mut aux = vec::with_capacity(mid+1);
    __mergesort(a, 0, a.len(), aux);

    fn __mergesort<T: Clone+Ord>(a: &mut[T], lo: uint, hi: uint, aux: &mut[T]){
        let mid = a.len()/2 as uint;
        if mid > 0 {
            __mergesort(a, lo, mid, aux);
            __mergesort(a, mid+1, hi, aux);
            __merge(a, mid, aux);
    }

        fn __merge<T: Clone+Ord>(a: &mut[T], lo: uint, mid: uint, hi: uint, aux: &mut[T]) {
            __clone_array(a, 0, mid+1, aux, 0);
            let mut i = 0;
            let mut j = 0;
            let mut k = 0;
            while k < a.len() {
                if j >= a.len()-mid {
                    __clone_array(a, k, a.len(), aux, 0);
                    break;
                } else if i >= mid {
                    break;
                }
                if aux[i] <= a[mid+j] {
                    a[k] = aux[i];
                    i += 1;
                } else {
                    a[k] = a[mid+j];
                    j += 1;
                }
                k += 1;
            }
        }

        fn __clone_array<T: Clone>(a: &[T], af: uint, al: uint, b: &mut[T], bf: uint){
            for i in range(bf, bf+al-af) {
                b[i] = a[af+i].clone();
            }
        }
    }
}

pub fn print<T: ToStr>(v: &[T]) {
    print!("[ ");
    for i in range(0, v.len()) {
        print!("{} ", v[i].to_str());
    }
    println!("]");
}

Here is the error that I get

sort.rs:44:23: 44:41 error: unresolved name vec::with_capacity.

sort.rs:44 let mut aux = vec::with_capacity(mid+1); ^~~~~~~~~~~~~~~~~~

error: aborting due to previous error

What am I doing wrong?

2
I think there's a few more lines in the error message above the stuff you pasted, could you paste them too? - huon
@dbaupp I edited the post. - Omid Nikta
Thanks! What version of Rust do you have? (The output of rustc -v.) - huon
rustc 0.10 host: x86_64-unknown-linux-gnu - Omid Nikta
Why the leading __? It’s entirely superfluous. - Chris Morgan

2 Answers

1
votes

with_capacity is not a function in std::vec, it’s a static method on the Vec type. Capitalise your v in vec::with_capacity (Vec is automatically in scope, being a part of the prelude).

0
votes

In the near-ish future, you'll use Cargo to manage including external dependencies.