-1

My struct UdpSocketBuffer has the following new method:

pub fn new<MS, PS>(metadata_storage: MS, payload_storage: PS) -> PacketBuffer<'a, 'b, H>
        where MS: Into<ManagedSlice<'a, PacketMetadata<H>>>,
              PS: Into<ManagedSlice<'b, u8>>,
    {

I'm passing, as arguments:

let rx_buffer = UdpSocketBuffer::new(Vec::new(), vec![0, 1024]);
let tx_buffer = UdpSocketBuffer::new(Vec::new(), vec![0, 1024]);

Here's how the conversion happens:

impl<'a, T: 'a> From<Vec<T>> for ManagedSlice<'a, T> {
    fn from(value: Vec<T>) -> Self {
        ManagedSlice::Owned(value)
    }
}

I'm getting the error:

literal out of range foru8rustc(overflowing_literals)

for the 1024 number. But I thought that the 0 is the value, and 1024 is just how many of 0s the vector will begin with. Why is Rust interpreting 1024 as u8?

Guerlando OCs
  • 1,886
  • 9
  • 61
  • 150
  • 1
    https://doc.rust-lang.org/std/macro.vec.html, you want `vec![0; 1024]`. – Stargateur Jun 11 '20 at 21:57
  • 1
    Does this answer your question? [Creating a vector of zeros for a specific size](https://stackoverflow.com/questions/29530011/creating-a-vector-of-zeros-for-a-specific-size) – ShadowRanger Jun 11 '20 at 22:03

1 Answers1

4

vec![0, 1024] is a two-element Vec containing 0 and 1024. You need a semi-colon to initialize a large Vec with a single repeated value, e.g. vec![0; 1024].

Example from linked docs:

Create a Vec from a given element and size:

  let v = vec![1; 3];
  assert_eq!(v, [1, 1, 1]);
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
  • I don't see the point to answer a bad question that have obvious duplicate answer https://stackoverflow.com/questions/29530011/creating-a-vector-of-zeros-for-a-specific-size – Stargateur Jun 11 '20 at 22:01
  • @Stargateur: New to Rust questions, didn't know of obvious duplicate. No one has flagged it as such, even now, let alone at the time I answered.. – ShadowRanger Jun 11 '20 at 22:02
  • 1
    I search to find it - -, you really think nobody ever wanted to create a vector of zero ? https://stackoverflow.com/search?q=%5Brust%5D+vector+create+zero – Stargateur Jun 11 '20 at 22:04
  • 1
    @Stargateur: In this case, the answer was addressing why it didn't do what they expected, not merely how to do what they were attempting to do. There's nothing wrong with providing such an answer, though had I known of the duplicate, I would have kept it as a comment elaborating on the duplicate (I wasn't thinking of it as a question about making a vector of zeroes specifically). Annoyingly, the people who cast close votes weren't consistent, so it's not even linked to the duplicate properly now (I asked mods to fix that, if they can). – ShadowRanger Jun 11 '20 at 22:06