我开始学习Rust并尝试实现一个反转字符串向量的函数。我找到了一个解决方案,但我不明白为什么它有效。
这个有效:
这个有效:
fn reverse_strings(strings:Vec<&str>) -> Vec<&str> {
let actual: Vec<_> = strings.iter().cloned().rev().collect();
return actual;
}
但是这个不行。
fn reverse_strings(strings:Vec<&str>) -> Vec<&str> {
let actual: Vec<_> = strings.iter().rev().collect(); // without clone
return actual;
}
错误消息
src/main.rs:28:10: 28:16 error: mismatched types:
expected `collections::vec::Vec<&str>`,
found `collections::vec::Vec<&&str>`
(expected str,
found &-ptr) [E0308]
有人能解释一下为什么吗?第二个函数中发生了什么?谢谢!