How can I create a vector of structs in a loop

I’m trying to learn Rust and am using the advent of code to do so. I’ve just started and have decided to have a vector containing "Elf" structs, of which each will contain a vector calory_items. I’m trying to create this from a text file and have the following code: use std::io::{self, BufReader}; use std::path::Path;… Read More How can I create a vector of structs in a loop

Is there a way to prevent try_from from consuming a mutable reference?

I have a struct struct Triangle { normal: Normal, vertices: [Vertex; 3], } that I’d like to deserialize from a Read type. I thought implementing TryFrom would be idiomatic since I’m turning a stream of bytes into a Triangle but it may fail. impl TryFrom<&mut dyn std::io::Read> for Triangle { type Error = std::io::Error; fn… Read More Is there a way to prevent try_from from consuming a mutable reference?

Implement `Copy` trait for `Option<Box<Struct>>`

I’m trying to implement a simple hash table with external chaining in Rust but having trouble with the table declaration. I declared the table as such: static mut _HASH_TABLE: [Option<Box<MemoryMap>>; (std::u16::MAX -1) as usize] = [None; (std::u16::MAX -1) as usize]; with the MemoryMap being a dynamic linked list pub struct MemoryMap { entry: SimpleEntry, next:… Read More Implement `Copy` trait for `Option<Box<Struct>>`

Why borrow persists after clone

I can’t understand why I am getting this error below: ` error[E0502]: cannot borrow `*s` as mutable because it is also borrowed as immutable –> src/main.rs:8:5 | 7 | let char_h = s.get(0..0).unwrap().clone(); | ———– immutable borrow occurs here 8 | s.replace_range(0..0, "h"); | ^^^^^^^^^^^^^^^^^^^^^^^^^^ mutable borrow occurs here 9 | println!("char before {char_h} string… Read More Why borrow persists after clone

How to get many references to one object, using SlotMap?

enum List<‘a> { Cons(isize, &’a List<‘a>), Nil, } fn main() { use List::*; use slotmap::SlotMap; let mut table = SlotMap::new(); let nil = table.insert(Nil); table.insert(Cons(1, table[nil])); } SlotMap<K: Key, V>‘s insert is pub fn insert(&mut self, value: V) -> K and its impl<K: Key, V> Index<K> is fn index(&self, key: K) -> &V So surely… Read More How to get many references to one object, using SlotMap?

Assign a struct implementation to another struct implementation

I’d like to keep an instance of the handlebars registry in a struct named "Mailer". The instance is to be initialized upon running the "new" function of the Mailer implementation. I do have massive problems in figuring out the proper ownership at each scope. use handlebars::Handlebars; pub struct Mailer<‘reg> { registry: &’reg handlebars::Handlebars<‘reg> } impl… Read More Assign a struct implementation to another struct implementation

How to fix `returns a value referencing data owned by the current function` when using std::str::Chars in Rust

I have a function tk_from_file that reads a file, and returns a struct Tokenizer, with a Peekable<Chars<‘a>> over the contents of that file. The variable content is from the Result<String> returned from std::fs::read_to_string However, when I try to create the struct and return it, I get the following error (E0515): 30 | return Tokenizer {… Read More How to fix `returns a value referencing data owned by the current function` when using std::str::Chars in Rust