使用enum Axes来限制Coordinate和Quaternion:
#[derive(Clone)]
pub enum Axes {
Coordinate {x: f64, y: f64, z: f64, reserve: Vec<f64>,},
Quaternion {x: f64, y: f64, z: f64},
}
impl Axes {
pub fn shift(&mut self, Sample: &Axes) -> () {
let Dup: Axes = self.clone();
match Dup {
Axes::Coordinate {x, y, z, reserve} => {
match &Sample {
Axes::Coordinate {x, y, z, reserve} => {
*self = Axes::Coordinate {x: *x, y: *y, z: *z, reserve: reserve.to_vec()};
}
_ …Run Code Online (Sandbox Code Playgroud) 我有一个带有类型参数的函数,U它返回一个Option<U>. U受 trait 的约束num::Num。这样,U可以是usize,u8,u16,u32,u64,u128,isize,等。
我如何匹配U?例如,
match U {
u8 => {},
u16 => {}
_ => {}
}
Run Code Online (Sandbox Code Playgroud)