I gave you a full example of correcting your mistakes.Code:use core::ops::Mul;
use core::ops::Sub;
use std::ops::Add;
struct Vector<T> {
x1: T,
x2: T,
x3: T,
}
/* сложение /
/ ошибка в Add<T> /
/ вы указали добавить тип T, хотя явно желали Vector<T> /
/ поэтому Add<T> -> Add<Vector<T>>*/
/* советую читать онлайн документацию /
// https://doc.rust-lang.org/std/ops/trait.Add.html
impl<T> Add</T/ Vector<T>> for Vector<T> where T: Add<Output = T> / мы описали where для того чтобы любой T внутри функции можно было сложить с T*/ {
type Output = Vector<T>;
fn add(self, rhs: Vector<T>) -> Vector<T> {
Vector {
x1: self.x1 + rhs.x1, /* у вас любой тип T, не обязательно у типа данных */
x2: self.x2 + rhs.x2, /* T есть сложение, опишите это в where !! */
x3: self.x3 + rhs.x3
} //; ошибка, вы хотите вернуть значение (используйте return или опустите знак ;)
}
}
// вычитание
// https://doc.rust-lang.org/std/ops/trait.Sub.html
impl<T> Sub<Vector<T>> for Vector<T> where T: Sub<Output = T> /* мы описали where для того чтобы любой T внутри функции можно было вычесть с T*/ {
type Output = Vector<T>;
fn sub(self, rhs: Vector<T>) -> Vector<T> {
Vector {
x1: self.x1 - rhs.x1,
x2: self.x2 - rhs.x2,
x3: self.x3 - rhs.x3
}
}
}
// умножение
// https://doc.rust-lang.org/std/ops/trait.Mul.html
impl<T> Mul<Vector<T>> for Vector<T> where T: Mul<Output = T> /* мы описали where для того чтобы любой T внутри функции можно было умножить с T*/ {
type Output = Vector<T>;
fn mul(self, rhs: Vector<T>) -> Vector<T> {
Vector {
x1: self.x1 * rhs.x1,
x2: self.x2 * rhs.x2,
x3: self.x3 * rhs.x3
}
}
}
/* все операторы сложения, умножения, .../
/ можно получить по пути /
/ https://doc.rust-lang.org/std/ops/index.html */
fn main() {
/* сумма */
let vector1 = Vector { x1: 5.6, x2: 7.2, x3: 2.5 };
let vector2 = Vector { x1: -0.1, x2: 10.9, x3: 3.3 };
let vector3 = vector1 + vector2;
println!("Сумма векторов: ({}, {}, {})", vector3.x1, vector3.x2, vector3.x3);
/* разница */
let vector1 = Vector { x1: 5.6, x2: 7.2, x3: 2.5 };
let vector2 = Vector { x1: -0.1, x2: 10.9, x3: 3.3 };
let vector4 = vector1 - vector2;
println!("Разница векторов: ({}, {}, {})", vector4.x1, vector4.x2, vector4.x3);
/* умножение */
let vector1 = Vector { x1: 5.6, x2: 7.2, x3: 2.5 };
let vector2 = Vector { x1: -0.1, x2: 10.9, x3: 3.3 };
let vector4 = vector1 * vector2;
println!("Умножение векторов: ({}, {}, {})", vector4.x1, vector4.x2, vector4.x3);
/*
Сумма векторов: (5.5, 18.1, 5.8)
Разница векторов: (5.699999999999999, -3.7, -0.7999999999999998)
Умножение векторов: (-0.5599999999999999, 78.48, 8.25)
*/
}
Councils:In the plant, stick to the general form of code insertion.Read additional online documentation https://doc.rust-lang.org/std/index.html There's plenty of books for newcomers.There's a formal Russian community for newcomers on the veget in the telega. https://t.me/rust_beginners_ru )/Completed/Put VsCode IDE with Rust plastic, don't write online.Go!