Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Design patterns 什么';重构代码以实现Rust中的合成的最佳方法是什么?_Design Patterns_Rust_Composition - Fatal编程技术网

Design patterns 什么';重构代码以实现Rust中的合成的最佳方法是什么?

Design patterns 什么';重构代码以实现Rust中的合成的最佳方法是什么?,design-patterns,rust,composition,Design Patterns,Rust,Composition,下面有很多重复的代码。重构它的最佳方法是什么 我来自JavaScript,在那里我使用组合来实现相同的结果,但在Rust中,我似乎不得不重新实现很多代码 我知道有一种使用宏的方法,但是如果逻辑更复杂,我最终会得到一个大的可执行文件,因为宏是内联的 struct Shape { x: i32, y: i32, } struct Circle { x: i32, y: i32, radious: i32, } struct Square { x:

下面有很多重复的代码。重构它的最佳方法是什么

我来自JavaScript,在那里我使用组合来实现相同的结果,但在Rust中,我似乎不得不重新实现很多代码

我知道有一种使用宏的方法,但是如果逻辑更复杂,我最终会得到一个大的可执行文件,因为宏是内联的

struct Shape {
    x: i32,
    y: i32,
}

struct Circle {
    x: i32,
    y: i32,
    radious: i32,
}

struct Square {
    x: i32,
    y: i32,
    width: i32,
    height: i32,
}

trait Advance {
    fn advance(&mut self);
}

impl Advance for Shape {
    fn advance(&mut self) {
        self.x += 1;
    }
}

impl Advance for Square {
    fn advance(&mut self) {
        self.x += 1;
    }
}

impl Advance for Circle {
    fn advance(&mut self) {
        self.x += 1;
    }
}

fn main() {
    println!("Hello World!!");

    let mut shape = Shape { x: 0, y: 0 };
    let mut square = Square {
        x: 0,
        y: 0,
        width: 10,
        height: 10,
    };
    let mut circle = Circle {
        x: 0,
        y: 0,
        radious: 5,
    };

    while shape.x < 10 {
        shape.advance();
        circle.advance();
        square.advance();
        println!("shape {}", shape.x);
        println!("circle {}", circle.x);
        println!("square {}", square.x);
        println!("---------------------");
    }
}
struct-Shape{
x:i32,
y:i32,
}
结构圆{
x:i32,
y:i32,
无线电:i32,
}
结构广场{
x:i32,
y:i32,
宽度:i32,
高度:i32,
}
性状进展{
fn预付款(&mut-self);
}
形状预测{
fn预付款(&mut-self){
自身x+=1;
}
}
广场预演{
fn预付款(&mut-self){
自身x+=1;
}
}
圆的impl前进{
fn预付款(&mut-self){
自身x+=1;
}
}
fn main(){
println!(“你好,世界!!”);
设mut-shape=shape{x:0,y:0};
让mut平方=平方{
x:0,,
y:0,
宽度:10,
身高:10,
};
让mut循环=循环{
x:0,,
y:0,
无线电:5,
};
而形状.x<10{
shape.advance();
循环前进();
正方形。前进();
println!(“shape{}”,shape.x);
println!(“圆圈{}”,圆圈.x);
println!(“square{}”,square.x);
println!(“--------------------------”);
}
}

无法扩展结构,但您可以将公共属性放入另一个结构,并且您的结构可以由这些公共结构组成。公共行为可以通过这些公共结构触发,如
Point

#[derive(Default)]
struct Point {
    x: i32,
    y: i32,
}

struct Shape {
    point: Point,
}

struct Circle {
    point: Point,
    radious: i32,
}

struct Square {
    point: Point,
    width: i32,
    height: i32,
}

trait Advance {
    fn advance(&mut self);
}

impl Advance for Point {
    fn advance(&mut self) {
        self.x += 1;
    }
}

fn main() {
    let mut shape = Shape {
        point: Point::default(),
    };
    let mut square = Square {
        point: Point::default(),
        width: 10,
        height: 10,
    };
    let mut circle = Circle {
        point: Point::default(),
        radious: 5,
    };

    while shape.point.x < 10 {
        shape.point.advance();
        circle.point.advance();
        square.point.advance();
        println!("shape {}", shape.point.x);
        println!("circle {}", circle.point.x);
        println!("square {}", square.point.x);
        println!("---------------------");
    }
}
#[派生(默认)]
结构点{
x:i32,
y:i32,
}
结构形状{
点:点,,
}
结构圆{
点:点,,
无线电:i32,
}
结构广场{
点:点,,
宽度:i32,
高度:i32,
}
性状进展{
fn预付款(&mut-self);
}
点的impl前进{
fn预付款(&mut-self){
自身x+=1;
}
}
fn main(){
让mut shape=shape{
点:点::默认值(),
};
让mut平方=平方{
点:点::默认值(),
宽度:10,
身高:10,
};
让mut循环=循环{
点:点::默认值(),
无线电:5,
};
而shape.point.x<10{
shape.point.advance();
圆.点.前进();
平方点前进();
println!(“shape{}”,shape.point.x);
println!(“圆{}”,圆.point.x);
println!(“square{}”,square.point.x);
println!(“--------------------------”);
}
}

@hellow请不要以“它属于代码审查”的自定义理由投票结束。堆栈溢出规则中没有任何东西可以证明这样的自定义原因,而草率的推理会使不适当的引用永久化。并不是所有关于分析代码的问题都是关于堆栈溢出的离题,也不是所有的代码审阅请求都是关于代码审阅的主题。取而代之的是,投票以过于宽泛或主要基于意见的方式结束。@Zeta“它属于另一个stackexchange网页”有一些选项,但codereview不是其中之一,所以我想我只是使用了一个自定义的理由来标记它。@hellow是的,这就是。这就是说:虽然代码可能是关于CR的主题,但由于缺少所需的上下文,当前关于CR的问题是非主题的。对于这些情况,如果你对一些解释感兴趣。这个问题已经被交叉发布了,对于这种开放式问题,哪一个地方更好。