我是 Rust 的新手,我正在尝试 Rustlings quiz2 问题。
下面是完整的代码供参考
// quiz2.rs
// This is a quiz for the following sections:
// - Strings
// - Vecs
// - Move semantics
// - Modules
// - Enums
// Let's build a little machine in the form of a function.
// As input, we're going to give a list of strings and commands. These commands
// determine what action is going to be applied to the string. It can either be:
// - Uppercase the string
// - Trim the string
// - Append "bar" to the string a specified amount of times
// The exact form of this will be:
// - The input is going to be a Vector of a 2-length tuple,
// the first element is the string, the second one is the command.
// - The output element is going to be a Vector of strings.
// No hints this time!
// I AM NOT DONE
pub enum Command {
Uppercase,
Trim,
Append(usize),
}
mod my_module {
use super::Command;
// TODO: Complete the function signature!
pub fn transformer(input: Vec<(String, Command)>) -> Vec<String> {
// TODO: Complete the output declaration!
let mut output: Vec<String> = vec![];
for (string, command) in input.iter() {
// TODO: Complete the function body. You can do it!
let s = match command {
Command::Uppercase => string.to_uppercase(),
Command::Trim => string.trim().into(),
Command::Append(i: &usize) => format!("{}{}", string.to_owned(), "bar".repeat(*i))
};
output.push(s)
}
output
}
}
#[cfg(test)]
mod tests {
// TODO: What do we need to import to have `transformer` in scope?
use crate::my_module::transformer;
use super::Command;
#[test]
fn it_works() {
let output = transformer(vec![
("hello".into(), Command::Uppercase),
(" all roads lead to rome! ".into(), Command::Trim),
("foo".into(), Command::Append(1)),
("bar".into(), Command::Append(5)),
]);
assert_eq!(output[0], "HELLO");
assert_eq!(output[1], "all roads lead to rome!");
assert_eq!(output[2], "foobar");
assert_eq!(output[3], "barbarbarbarbarbar");
}
}
特别是,我不确定为什么在
transformer
函数中,i
中的Command::Append(i)
是&usize
类型,而不是usize
的Command
枚举中定义的Append(usize)
。
具体来说,我指的是测验解决方案中的这一行:
Command::Append(i: &usize) => format!("{}{}", string.to_owned(), "bar".repeat(*i))
我问过chatgpt,这是原因,但我不确定它是否正确:
命令是参考的原因是因为 input 是包含 String 和 Command 枚举的元组向量。 String 是拥有的,可以从元组中移出, 但是 Command 枚举是不可复制的类型, 所以我们必须通过引用传递它以避免将它移出元组。 这就是为什么 command 是一个引用而不是一个拥有的值。
因为命令变量是一个引用...
transformer函数中,命令变量 是对 Command 枚举值的引用,因此当您 对其进行模式匹配并匹配 Command::Append(i) 案例, 我将引用存储在 Append 变体中的 usize 值。
这是由于通常所说的匹配人体工程学如果你这样匹配
match &Foo(99) {
Foo(x) => {},
}
编译器将隐式取消对
Foo
的引用,然后与ref
模式匹配:
match *&Foo(99) {
Foo(ref x) => {},
}
注意:编译器实际上不会尝试移动
Foo
或在那里插入取消引用,它只是说明发生了什么,引用被取消引用并创建对包含值的新引用。
这使 x
成为对 Foo
中包含的原始类型的引用。
这在您的代码中发生了两次:
for (string, command) in input.iter() {/*..*/}
将
input.iter()
的每个元素与 &(String, Command)
类型与模式 (string, command)
匹配,所以 command
将是 &Command
类型
同样的事情在你的比赛中再次发生:
match command {
//..
Command::Append(i) => {/*..*/}
}
隐式转换为类似的东西
match *command {
//..
Command::Append(ref i) => {/*..*/}
}
这就是为什么
i
在你的比赛中的类型是&usize