Questions tagged [vec]

78 questions
17
votes
3 answers

Error[E0277]: the type `[u32]` cannot be indexed by `u32`

What am I doing wrong with the variable i below? Why does the compiler say I cannot index a Vec with a u32 and how do I fix it? fn main() { let a: Vec = vec![1, 2, 3, 4]; let number: u32 = 4; let mut count = 0; for i in…
Masaki Imai
  • 181
  • 1
  • 1
  • 3
9
votes
4 answers

Checking a Vec to see if it's all zero?

I have many 4KiB buffers, which have a 50% chance of containing only zero values. The non-zero buffers will typically have a non-zero byte early in the buffer. fn is_zero(buf: &Vec) -> bool { for byte in buf.into_iter() { if *byte…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
7
votes
2 answers

How to have a vec of Boxes which implement 2 traits?

In rust I would like to have a vec containing items which implement 2 traits. However when I try to implement it like so, I get the error only auto traits can be used as additional traits in a trait object: let mut v : Vec
geeks_kick
  • 151
  • 7
7
votes
2 answers

How to write file from Vec?

I have a base64 image and got Vec from it and write them to a file. Here is my code: let file_contents_base64: Vec = image_base64::from_base64( String::from(&file_contents_string_vec[0]) ); I want to write the file_contents_base64…
Pixel Coder
  • 139
  • 1
  • 1
  • 6
6
votes
1 answer

How to Convert vector into json in rust

everyone, I just came into contact with rust recently, and now I want to use rust to write a static website. Now I have a very basic problem. The code is as follows: pub struct Post { title: String, created: String, link: String, …
lanqy
  • 3,001
  • 5
  • 25
  • 18
5
votes
1 answer

Can I hold a pointer (unsafely) to an element of Vec while the Vec is being moved?

I'm implementing an algorithm, and in order to maintain the desired time complexity, I would like to hold a pointer to an element of a Vec while the Vec is being moved. Specifically, something like this: fn main() { let mut v: Vec =…
Bernard
  • 5,209
  • 1
  • 34
  • 64
5
votes
3 answers

How can I retain vector elements with their original index?

If I have a Vec I can iterate over elements using an index via v.iter().enumerate(), and I can remove elements via v.retain(). Is there a way to do both at once? In this case the index could no longer be used to access the element - it would be the…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
4
votes
0 answers

Why can I initialize but not assign a &vec![]?

What is the difference between assign after binding to var and direct assign a &Vec. let mut v2 = &vec![1,2,3]; let tv = &vec![2,3,4]; v2 = tv; // different from // v2 = &vec![2, 3, 4]; // uncomment this will error println!("{:?}", v2); borrow…
BruceChar
  • 83
  • 1
  • 5
4
votes
3 answers

Is Vec::splice() efficient when the length doesn't change?

When you use Vec::splice() to replace part of a Vec, is it smart enough to specially handle the case where the replacement is the same length as the chunk it is replacing, or should I handle that myself? Is there any point doing this, or is…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
4
votes
4 answers

Rustlings Exercise Traits2, Implement Trait on Vec

The exercise requires me to Implement the trait to Vec. The tests are there and they are failing, which is a good place to start. I've done the trait implementation for the String and it was easy, Vec is another story. I'm not sure what the method…
woss
  • 933
  • 1
  • 11
  • 20
3
votes
1 answer

Why can't you convert containers using .into() if the elements can be converted?

If I have a Vec I can't convert it into Vec directly using .into() even if I can convert T into U using .into(). For example this code does not compile: use std::convert::From; struct A { x: i32, } struct B { y: u64, } impl From
Timmmm
  • 88,195
  • 71
  • 364
  • 509
3
votes
2 answers

Builtin for checking if vec contains specified element

Let's say, I have a vec containing a list of integers that may be non-continuous (due to element being removed from database). Example: $occupiedNumbers = vec[1, 2, 3, 5, 6, 8, 10, 11, 12, 13, 15, 16]; Now what I need is to check if this vec…
Lukasz032
  • 354
  • 4
  • 14
3
votes
1 answer

Vector pop() returns Option

I am beginner in rust. I see pop() method of vector returns
Fel AA
  • 303
  • 5
  • 16
2
votes
2 answers

Is it better to return an Option of a Vec, or just a Vec?

Suppose I am writing a function that takes a bunch of string and filters out the "bad" ones. The function then returns some strings, but there is a chance that all the strings are filtered out. So I am wondering, should I use to Option here? I could…
Jim
  • 3,821
  • 1
  • 28
  • 60
2
votes
1 answer

Difficulty in trying to implement a type-safe `at` for length-indexed vectors

I have just learnt about the DataKinds extension, type-level literals, and found out that type-levels natural numbers can be compared using constraints provided in the Data.Type.Ord such as (>), and manipulated using type-level operations provided…
Futarimiti
  • 551
  • 2
  • 18
1
2 3 4 5 6