Questions tagged [vec]

78 questions
2
votes
1 answer

What to use instead of `std::lower_bound` and `std::upper_bound` in Rust?

What we have in C++ C++ has two STL functions: std::lower_bound and std::upper_bound std::lower_bound finds first position of searched value if it exists, or position of first value greater. std::upper_bound finds first position with greater value…
2
votes
1 answer

Why elements should be dropped before dropping vec in rust

I am trying to implement a vector. I found this code where the writer deallocates a vector: impl Drop for MyVec { fn drop(&mut self) { if self.cap != 0 { while let Some(_) = self.pop() { } let layout =…
user19291301
  • 127
  • 8
2
votes
1 answer

How can I make Rust, with the Rodio crate, load multiple sources in a Vec, so I can play them later as needed without having to load them every time?

I am using Rust with the Rodio crate and I wanted to make a Vec of loaded sources to use whenever they are needed, so that the program doesn't need to load it every time. I've made a SoundHandler class that contains an OutputStream, an…
Felipe
  • 23
  • 5
2
votes
1 answer

How to return a Result?

I want a function to take in a reference to input and return a value if the input is valid or an error if its invalid. Here's my attempt but I get an error: use std::num::ParseIntError; fn get_fourth(input: &Vec) -> Result
curlywei
  • 682
  • 10
  • 18
2
votes
2 answers

How to modify the &mut self contents and return self

I am having a struct and respective impl (add_account) as well. Struct pub struct Departments { pub id: String, pub linked_accounts: Vec, } Impl impl Departments { pub fn add_account(&mut self, acct: Account) -> Self { …
Arul
  • 23
  • 4
2
votes
2 answers

How to check if a value in a Vec is None?

I would like to use the fact that Vec.last() returns a None if there is nothing at that index but that index still exists. The problem is that I can't find a way to compare the result to anything inside of a while or if. I can however compare it if…
Merlin1846
  • 43
  • 6
2
votes
1 answer

Read::read_exact does not fill buffer

I have the following code: use std::io::{Cursor, BufReader, Read}; fn main() { let string = String::from("Cello"); let bytes = string.as_bytes(); let cursor = Cursor::new(bytes); let mut reader = BufReader::new(cursor); let mut…
abipip
  • 21
  • 2
2
votes
1 answer

How can I modify a part of bigger slice (or Vec) by passing another smaller slice?

How can I correctly implement this code below? let mut bigger: [u8; 100] = [0u8; 100]; let smaller: [u8; 3] = [1, 2, 3]; // do something like: // bigger[0..3] = smaller;
2
votes
1 answer

How to turn two nested HashMaps in a Vec of tuples without for loops?

Take the following data type: let mut items = HashMap::>::new(); I successfully managed to turn it into a vector of tuples like this: let mut vector_of_tuples: Vec<(u64, u64, bool)> = vec![]; for (outer_id, inner_hash_map)…
Paul Razvan Berg
  • 16,949
  • 9
  • 76
  • 114
2
votes
1 answer

Reading a large file line by line and avoiding utf8 errors in Rust

I have a really large file that "should" consist of JSON strings. However, when I use the following code, I get a "stream did not contain valid UTF8". let file = File::open("foo.txt")?; let reader = BufReader::new(file); for line in reader.lines()…
1
vote
1 answer

How did it get converted into 1 line of code from 3 lines? Remapping uv and fixing aspect ratio in shaders

How would you get from this vec2 uv = fragCoord/iResolution.xy; // <0, 1> uv -= 0.5; // <-0.5,0.5> uv.x *= iResolution.x/iResolution.y; // fix aspect ratio to this? vec2 uv = (fragCoord - .5 * iResolution.xy) / iResolution.y; // Condense 3 lines…
1
vote
1 answer

Sort a vector of structs by an f64 value

I am trying to sort a vector of Substance structs: #[derive(Debug, PartialOrd, PartialEq)] struct Substance { price: i32, mass_kg: i32, price_per_kg: f64, } by price_per_kg my current sorting code is: // `substances` is a `vec` of…
Pioneer_11
  • 670
  • 4
  • 19
1
vote
2 answers

rust elementwise diff of vec?

What's the most idiomatic way to get the Rust vec equivalent of this Python code? import numpy as np a = np.arange(5) a_diff = np.diff(a) # this is the thing I'm trying to emulate in Rust print(a_diff) # [1 1 1 1] I can figure out multiple…
Chad
  • 1,434
  • 1
  • 15
  • 30
1
vote
1 answer

Ranges (double-dot ".." operator) with multiplication operator

I recently came across this expression in some code written by GUILLAUME ENDIGNOUX, which produces a vector of 10 numbers: (10 * i..10 * (i + 1)).collect() Gives: 420, 421, 422, 423, 424, 425, 426, 427, 428, 429 Here is a working example, where I…
1
vote
2 answers

How to destructure n items from a vec into variables?

In JavaScript, I can destructure an array in the following way: const [first, second, ...rest] = myArray Is there a similar way to achieve this in rust? If I only want one element, it's easy. I can do: let first = my_vec[0]; or if I make an array…
Emanuel Lindström
  • 1,607
  • 16
  • 25