0

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?

6
  • I think there's a few more lines in the error message above the stuff you pasted, could you paste them too? Commented Jun 18, 2014 at 12:46
  • Thanks! What version of Rust do you have? (The output of rustc -v.) Commented Jun 18, 2014 at 12:56
  • rustc 0.10 host: x86_64-unknown-linux-gnu Commented Jun 18, 2014 at 13:06
  • Why the leading __? It’s entirely superfluous. Commented Jun 18, 2014 at 13:25
  • @cris-morgan It is just for me to remind me it is an internal function and should not be used outside. Commented Jun 18, 2014 at 13:57

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Wow, I can't believe that was it. Thanks for the documentation too.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.