如何unused_variables从以下代码中删除警告?
pub enum Foo {
Bar {
a: i32,
b: i32,
c: i32,
},
Baz,
}
fn main() {
let myfoo = Foo::Bar { a: 1, b: 2, c: 3 };
let x: i32 = match myfoo {
Foo::Bar { a, b, c } => b * b,
Foo::Baz => -1,
};
assert_eq!(x, 4);
}
Run Code Online (Sandbox Code Playgroud)
我知道我可以在某一点之后忽略struct成员:
Foo::Bar { a, .. } => // do stuff with 'a'
Run Code Online (Sandbox Code Playgroud)
但我无法在任何地方找到解释如何忽略单个struct成员的文档.
这是我试图解决的编码问题...我有一个基类,让我们说动物,它有两个子类,比如Dog和Cat.我的类Animal有一个方法,make_baby(),Dog和Cat都将继承.我无法解决的诀窍是我希望返回值是调用函数但具有不同属性值的子类的新实例,即Dog.make_baby()应该返回一个新的Dog和Cat.make_baby( )将返回一只新猫.
我以前尝试返回"type(self)()",但这并不好,因为type()返回一个类型对象,而不是一个类.
这是完整的示例代码:
Class Animal():
def __init__(self, color):
self.color = color
def make_baby():
new_color = rand_color # a randomly chosen color
return #??? new class of the same type that called the method
Class Dog(Animal):
def pet():
print '*pant*'
Class Cat(Animal):
def pet():
print 'purrr'
Run Code Online (Sandbox Code Playgroud)
所以我想避免为Dogs和Cats编写一个make_baby()方法,因为这个方法是除了返回的类之外的方法完全一样.我还想避免一堆if语句,因为我想为Animal制作任意大量的子类.