Vector allocation

Here is my following code.

Basically I want to store an array of Address objects.
Since struct Array has String item, the compiler could not
find the size of each element of the array and hence did not compile.

How to over come ?.

I want to store the address objects in a vector.

#[derive(Debug)]
struct Address {

        Name : String,
        City : String,
        Pin : u32,
}

fn main()
{

    let addr_list: Vec<[Address]> = vec![];

    let address0 = Address{

                    Name: String::from("S.Gopinath"),
                    City: String::from("Chennai"),
                    Pin: 600090,
    };

    println!("{:?}", address0);
}

Please use code blocks to post code. It should look like this:

```
// put your code here, between the "fences"
```

Vec<[Address]> denotes a Vec of [Address]es. That's why the error message includes

help: the trait `std::marker::Sized` is not implemented for `[Address]`

You probably meant Vec<Address>.

1 Like

Here is your code run through rustfmt:

#[derive(Debug)]
struct Address {
    Name: String,
    City: String,
    Pin: u32,
}

fn main() {
    let addr_list: Vec<[Address]> = vec![];

    let address0 = Address {
        Name: String::from("S.Gopinath"),
        City: String::from("Chennai"),
        Pin: 600090,
    };
    println!("{:?}", address0);
}

You get this error:

error[E0277]: the size for values of type `[Address]` cannot be known at compilation time
 --> src/main.rs:9:20
  |
9 |     let addr_list: Vec<[Address]> = vec![];
  |                    ^^^^^^^^^^^^^^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `[Address]`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required by `std::vec::Vec`

You're trying to store a list of addresses inside of a vector, which is already a list. This is probably not what you want. Admittedly, the error message is not super helpful here either. /cc @ekuber

If you want to store the Addresses in the vector, then do that:

#[derive(Debug)]
struct Address {
    Name: String,
    City: String,
    Pin: u32,
}

fn main() {
    let addr_list: Vec<Address> = vec![];

    let address0 = Address {
        Name: String::from("S.Gopinath"),
        City: String::from("Chennai"),
        Pin: 600090,
    };
    
    println!("{:?}", address0);
}

This of course doesn't actually put them in the vector yet, but it does compile! one step closer.

3 Likes

Hi,

Thanks.......

hi.. thanks ..

//println!("Thanks.. will do that");

Siir, Thanks.. for pointing out the error I made.

I understand. But I still have a question/clarification.

In my example, in my Address struct I have 3 items 2 with string and 1 integer
so in the heap, at first level, it takes space for 2 pointers (for strings) and 4 bytes for integer
and hence size is deterministic at this level.

In my example I put Vec<[Address]> which is Vector_Array of Address arrays and
compiler has no clue how much mem to allocate as each item of the vector array
need a space which is non-deterministic as the size of the array is unknown.

Is this is right interpretation ?>

1 Like

Yes, all of that is correct (modulo a few grammar errors, but I think I can tell what you're trying to say).

It may help to add that Vec<&[Address]> is an actually useful type that will compile, because &[T] has a size known at compile-time (pointer + length, like Vec without the capacity) even though [T] does not.

Thanks....