Skip to content

Instantly share code, notes, and snippets.

@willcrichton willcrichton/ Secret
Created Feb 4, 2018

What would you like to do?
use std::heap::{Heap, Layout, Alloc};
use std::slice;
struct Vec2 {
data: Box<[isize]>,
length: usize,
capacity: usize,
impl Vec2 {
fn new() -> Vec2 {
let v = Vec2 {
data: Box::new([0]),
length: 0,
capacity: 1,
return v;
fn push(&mut self, n: isize) {
if self.length == self.capacity {
let new_capacity = self.capacity * 2;
let mut new_data = unsafe {
let ptr = Heap::default()
.unwrap() as *mut isize;
Box::from_raw(slice::from_raw_parts_mut(ptr, new_capacity))
for i in 0..self.length {
new_data[i] =[i];
} = new_data;
self.capacity = new_capacity;
}[self.length] = n;
self.length += 1;
fn main() {
let mut vec: Vec2 = Vec2::new();
let n: &isize = &[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.