Can I create an immutable vector from another immutable vector in Rust without copying?

Advertisements I’m new to Rust and write a library for linear algebra on binary matrices. I have a sparse matrix struct storing a matrix in column major format (CscMatrix). The CscMatrix stores the positions of ones in each column, each column takes up a contiguous segment in the ‘ones’ vector. The index of the first… Read More Can I create an immutable vector from another immutable vector in Rust without copying?

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

Advertisements 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;… Read More Is there a way to prevent try_from from consuming a mutable reference?

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

Advertisements 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,… Read More Implement `Copy` trait for `Option<Box<Struct>>`

Why borrow persists after clone

Advertisements 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}… Read More Why borrow persists after clone

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

Advertisements 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… Read More How to get many references to one object, using SlotMap?

Assign a struct implementation to another struct implementation

Advertisements 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> }… Read More Assign a struct implementation to another struct implementation