如何使结构中的向量使用“add assign”特性?

huangapple go评论54阅读模式
英文:

How should I get the add assign trait to work for a vector within a struct?

问题

New to rust. Would like to understand why this issue is occurring and how to fix. I don't think I can change the signature for the implementation of the trait method either which makes this more harder to fix.

use std::ops::{AddAssign};

pub struct ColumnVector {
    pub data: Vec<f32>
}

impl AddAssign for &ColumnVector {
    fn add_assign(&mut self, other: Self) {
        for (index, elem) in other.data.iter().enumerate() {
            self.data[index] += elem;
        }
    }
}

fn main() {

}
英文:

New to rust. Would like to understand why this issue is occuring and how to fix. I don't think I can change the signature for the implementation of the trait method either which makes this more harder to fix.

use std::ops::{AddAssign};

pub struct ColumnVector {
    pub data: Vec&lt;f32&gt;
}

impl AddAssign for &amp;ColumnVector {
    fn add_assign(&amp;mut self, other: Self) {
        for (index, elem) in other.data.iter().enumerate() {
            self.data[index] += elem;
        }
    }
}

fn main() {
    
}

答案1

得分: 1

Using the &amp; creates an immutable reference to ColumnVector and its fields, so you cannot get a mutable reference to update it.

You could do:

pub struct ColumnVector {
    pub data: Vec<f32>
}

impl AddAssign for &mut ColumnVector {
    fn add_assign(&mut self, other: Self) {
        for (index, elem) in other.data.iter().enumerate() {
            self.data[index] += elem;
        }
    }
}

fn main() {

}

if you needed the borrow, but you probably just want to remove the &amp; entirely.

For context:

You can either have n-number of immutable borrows, or a single mutable one. There are ways around this (read about interior mutability) but generally you must have exclusive access to a variable to change it.

When you borrowed ColumnVector, you could not later edit the field because you did not own the data.

英文:

Using the &amp; creates an immutable reference to ColumnVector and its fields, so you cannot get a mutable reference to update it.

You could do:


pub struct ColumnVector {
    pub data: Vec&lt;f32&gt;
}


impl AddAssign for &amp;mut ColumnVector {
    fn add_assign(&amp;mut self, other: Self) {
        for (index, elem) in other.data.iter().enumerate() {
            self.data[index] += elem;
        }
    }
}

fn main() {
    
}

if you needed the borrow, but you probably just want to remove the &amp; entirely.

For context:

You can either have n-number of immutable borrows, or a single mutable one. There are ways around this (read about interior mutability) but generally you must have exclusive access to a variable to change it.

When you borrowed ColumnVector, you could not later edit the field because you did not own the data.

huangapple
  • 本文由 发表于 2023年3月4日 00:51:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75629817.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定