Basit bir yardımcı yöntemden, aşırı yüklenme kullanan bir sınıf kullanırken bir derleme hatası alıyorum. İşte kendine yeten bir testtir (benim gerçek koddan basitleştirilmiş, ama yine de sorunu gösteren): Ben Rust 1.8 ile bu derlemek çalışıyorum"Ödünç alınan içeriğin dışına çıkamıyor" kullanıcı aşırı yüklendi
use std::ops::{Add, Sub, Neg, Mul, Div};
#[derive(Debug, Eq, PartialEq)]
pub struct Money {
cents: i64,
}
impl Money {
pub fn new(cents: i64) -> Money {
Money { cents: cents }
}
}
impl Add for Money {
type Output = Money;
fn add(self, other: Money) -> Money {
Money { cents: self.cents + other.cents }
}
}
impl Mul<Money> for f64 {
type Output = Money;
fn mul(self, rhs: Money) -> Money {
Money { cents: (self * rhs.cents as f64) as i64 }
}
}
#[derive(Debug)]
pub struct AbsOrPerc {
pub absolute: Money,
pub percent: f64,
}
impl AbsOrPerc {
pub fn new(abs: Money, perc: f64) -> AbsOrPerc {
AbsOrPerc {
absolute: abs,
percent: perc,
}
}
pub fn total(&self, basis: Money) -> Money {
// This works:
// Money::new((self.absolute.cents as f64 + self.percent * basis.cents as f64) as i64)
// This doesn't:
self.absolute + self.percent * basis
}
}
ama bu hatayı alıyorum:
src/lib.rs:42:5: 42:9 error: cannot move out of borrowed content [E0507]
src/lib.rs:42 self.absolute + self.percent * basis
Rust Kitabını ve sahiplik ve borçlanma ile ilgili parçaları okudum. Bu soru, ör .:
Cannot move out of borrowed content
hata aynı iken, koşullar farklı olduğu için kendi soru yineleniyor sanmıyorum hakkında burada StackOverflow'daki sayıda soru okudum. Ayrıca, diğer soruların buna nasıl uygulandığını bilsem, sormam gerekmeyecek. :-)
Benim sorum şu: Bu hatayı nasıl çözebilirim? &self
'u self
olarak değiştirmek istemiyorum, çünkü bu başka sorunlara neden oluyor. Sadece sorunu çözmekle kalmanın yanı sıra, Rust'un ne korktuğunu bilmek isterim. Burada herhangi bir tehlike görmüyorum.
bakınız http://stackoverflow.com/q/28595075/155423, http://stackoverflow.com/q/29926724/155423, http://stackoverflow.com/q/30974593/155423, http://stackoverflow.com/q/28527702/155423, http://stackoverflow.com/q/34621969/155423 ve http://stackoverflow.com/Q/28843931/155423. – Shepmaster