A way to resize a stack (or other dynamically changing types)

First, I read this question:

Problem

The selected answer simply tells the questioning user to use the standard library instead of explaining the implementation, which is great if my goal is to build something. Except that I am trying to learn about the implementation of the stack by following the data structure tutorial written for Java (Robert Sedgwick and Kevin Wayne Algorithms), where they implement the stack by resizing the array (Page 136).

I execute the resize method, and it turns out that the size of the array must be a constant expression.

meta: are arrays in rust called slices?

use std::mem;

struct DynamicStack<T> {
  length: uint,
  internal: Box<[T]>,
}

impl<T> DynamicStack<T> {
  fn new() -> DynamicStack<T> {
    DynamicStack {
      length: 0,
      internal: box [],
    }
  }

  fn resize(&mut self, new_size: uint) {
    let mut temp: Box<[T, ..new_size]> = box unsafe { mem::uninitialized() };

    // ^^ error: expected constant expr for array 
    // length: non-constant path in constant expr

    // code for copying elements from self.internal

    self.internal = temp;
  }
}

For brevity, the compiler error was

.../src/lib.rs:51:23: 51:38 error: expected constant expr for array length: non-constant path in constant expr
.../src/lib.rs:51     let mut temp: Box<[T, ..new_size]> = box unsafe { mem::uninitialized() };
                                        ^~~~~~~~~~~~~~~
.../src/lib.rs:51:23: 51:38 error: expected constant expr for array length: non-constant path in constant expr
.../src/lib.rs:51     let mut temp: Box<[T, ..new_size]> = box unsafe { mem::uninitialized() };
                                        ^~~~~~~~~~~~~~~

Question

, , ( )? , ?

,

struct DynamicStack<T> {
  length: uint,
  internal: Box<Optional<T>>
}

, , .

( )

fn resize(&mut self, new_size: uint) {
  let mut temp: Box<[T]> = box [];
  let current_size = self.internal.len();

  for i in range(0, current_size) {
    temp[i] = self.internal[i];
  }
  for i in range(current_size, new_size) {
    temp[i] = unsafe { mem::uninitialized() };
  }

  self.internal = temp;
}

.../src/lib.rs:55:17: 55:21 error: cannot move out of dereference of `&mut`-pointer
.../src/lib.rs:55       temp[i] = self.internal[i];
                                  ^~~~
.../src/lib.rs:71:19: 71:30 error: cannot use `self.length` because it was mutably borrowed
.../src/lib.rs:71       self.resize(self.length * 2);
                                    ^~~~~~~~~~~
.../src/lib.rs:71:7: 71:11 note: borrow of `*self` occurs here
.../src/lib.rs:71       self.resize(self.length * 2);
                        ^~~~
.../src/lib.rs:79:18: 79:22 error: cannot move out of dereference of `&mut`-pointer
.../src/lib.rs:79     let result = self.internal[self.length];
                                   ^~~~
.../src/lib.rs:79:9: 79:15 note: attempting to move value to here
.../src/lib.rs:79     let result = self.internal[self.length];
                          ^~~~~~
.../src/lib.rs:79:9: 79:15 help: to prevent the move, use `ref result` or `ref mut result` to capture value by reference
.../src/lib.rs:79     let result = self.internal[self.length];

, , C/++

+4
1

, Rust , ?

, , . , ! Rust .

, , , , Rust . , (, ).

, , . , Vec ! Vec.

, N . , . , . , , , !

Edit

, , Vec , , Java. Option, .

+4

All Articles