英文:
expected struct `String` found struct `Vec<&String>`
问题
您的代码中有一些错误,我会帮助您进行修正,以下是修正后的代码:
fn reverse_words(words: &str) -> String {
let mut split = words.split_whitespace();
let vec: Vec<&str> = split.collect();
let mut vec2 = Vec::new();
for word in vec {
vec2.push(word.chars().rev().collect::<String>());
}
return vec2.join(" ");
}
在修正后的代码中,我做了以下更改:
- 将
&
替换为正常的&
,以使代码有效。 - 修正了
vec2
的类型,将其更改为Vec<String>
,以存储反转后的单词。 - 使用
join
方法将反转后的单词连接成一个字符串,并在单词之间添加空格。
这样,您的函数应该能够将输入的字符串中的单词反转,并在它们之间添加空格,然后返回一个拥有的字符串。
英文:
I have a function to reverse characters in words and I need to know how to convert a Vec<&String> into a regular owned String (with spaces between the words).
fn reverse_words(words: &str) -> String {
let mut split = words.split_whitespace();
let vec: Vec<&str> = split.collect();
let vec2 = vec!();
for word in vec {
vec2.push(&word.chars().rev().collect::<String>());
}
return vec2;
}
.
error[E0308]: mismatched types
--> src/lib.rs:8:12
|
1 | fn reverse_words(words: &str) -> String {
| ------ expected `String` because of return type
...
8 | return vec2;
| ^^^^ expected struct `String`, found struct `Vec`
|
= note: expected struct `String`
found struct `Vec<&String>`
For more information about this error, try `rustc --explain E0308`.
error: could not compile `challenge` due to previous error
NOTE: Line numbers in error messages can be incorrect due to concatenation.
答案1
得分: 2
以下是你的代码,经过最少的更改以使其正常工作。你只需要在最后的语句中添加.join(' ')
:
fn reverse_words(words: &str) -> String {
let split = words.split_whitespace();
let vec: Vec<&str> = split.collect();
let mut vec2 = vec![];
for word in vec {
vec2.push(word.chars().rev().collect::<String>());
}
vec2.join(" ")
}
fn main() {
let sentence = "hello world";
assert_eq!(reverse_words(sentence), "olleh dlrow");
}
这是一个效率低下的实现,需要大量的分配。您可以链接Iterator
trait的方法,以获得不需要太多分配的解决方案:
fn reverse_words(words: &str) -> String {
words
.split_whitespace()
.map(|w| w.chars().rev().chain([' ']))
.flatten()
.take(words.len())
.collect::<String>()
}
fn main() {
let sentence = "hello world";
assert_eq!(reverse_words(sentence), "olleh dlrow");
}
英文:
Here is a version of your code with minimal changes to make it work. You basically only need to add a .join(' ')
to your last statement:
fn reverse_words(words: &str) -> String {
let split = words.split_whitespace();
let vec: Vec<&str> = split.collect();
let mut vec2 = vec![];
for word in vec {
vec2.push(word.chars().rev().collect::<String>());
}
vec2.join(" ")
}
fn main() {
let sentence = "hello world";
assert_eq!(reverse_words(sentence), "olleh dlrow");
}
This is an inefficient implementation requiring a lot of allocation though. You can chain methods of the Iterator
trait to get a solution that does not require as much allocation:
fn reverse_words(words: &str) -> String {
words
.split_whitespace()
.map(|w| w.chars().rev().chain([' ']))
.flatten()
.take(words.len())
.collect::<String>()
}
fn main() {
let sentence = "hello world";
assert_eq!(reverse_words(sentence), "olleh dlrow");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论