gpt4 book ai didi

RuSTLings 练习 Traits2,在 Vec 上实现 Trait

转载 作者:行者123 更新时间:2023-12-03 11:29:04 25 4
gpt4 key购买 nike

练习要求我将特征实现到 Vec。测试在那里,但它们失败了,这是一个很好的起点。我已经完成了 String 的特征实现,这很容易,Vec 是另一回事。我不确定该方法需要返回什么,它在各种返回时都失败了。我提供原始代码、我的尝试以及我尝试时遇到的错误。希望这就足够了。

来自 RuSTLings 存储库的原始代码:

// traits2.rs
//
// Your task is to implement the trait
// `AppendBar' for a vector of strings.
//
// To implement this trait, consider for
// a moment what it means to 'append "Bar"'
// to a vector of strings.
//
// No boiler plate code this time,
// you can do this!

// I AM NOT DONE

trait AppendBar {
fn append_bar(self) -> Self;
}

//TODO: Add your code here




#[cfg(test)]
mod tests {
use super::*;

#[test]
fn is_vec_pop_eq_bar() {
let mut foo = vec![String::from("Foo")].append_bar();
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
}

}

我尝试解决它:

// traits2.rs
//
// Your task is to implement the trait
// `AppendBar' for a vector of strings.
//
// To implement this trait, consider for
// a moment what it means to 'append "Bar"'
// to a vector of strings.
//
// No boiler plate code this time,
// you can do this!

// I AM NOT DONE
use std::clone::Clone;
trait AppendBar {
fn append_bar(&mut self) -> Self;
}

//TODO: Add your code here
impl<T: Clone> AppendBar for Vec<T> {
fn append_bar(&mut self) -> Self {
let bar: T = String::from("Bar");
self.to_vec().push(bar)
// self.to_vec()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn is_vec_pop_eq_bar() {
let mut foo = vec![String::from("Foo")].append_bar();
assert_eq!(foo, vec![String::from("Foo"), String::from("Bar")]);
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
assert_eq!(foo.pop().unwrap(), String::from("Foo"));
}
}

编译为错误:


! Compiling of exercises/traits/traits2.rs failed! Please try again. Here's the output:
error[E0308]: mismatched types
--> exercises/traits/traits2.rs:22:22
|
20 | impl<T: Clone> AppendBar for Vec<T> {
| - this type parameter
21 | fn append_bar(&mut self) -> Self {
22 | let bar: T = String::from("Bar");
| - ^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found struct `std::string::String`
| |
| expected due to this
|
= note: expected type parameter `T`
found struct `std::string::String`
= help: type parameters must be constrained to match other types
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error[E0308]: mismatched types
--> exercises/traits/traits2.rs:23:9
|
21 | fn append_bar(&mut self) -> Self {
| ---- expected `std::vec::Vec<T>` because of return type
22 | let bar: T = String::from("Bar");
23 | self.to_vec().push(bar)
| ^^^^^^^^^^^^^^^^^^^^^^^ expected struct `std::vec::Vec`, found `()`
|
= note: expected struct `std::vec::Vec<T>`
found unit type `()`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.

我已经阅读并重新阅读了书中建议的部分和特征,但它超出了我的理解范围。我确定这是一个简单的解决方案,但我看不到。

最佳答案

Aloso 的回答有出入。 Andre 也给了它。

当你接受 self 时:

fn append_bar(self) -> Self {
self.push("Bar".to_owned());
self
}

你正在接受一个可变的Vec:

let mut foo = vec![String::from("Foo")].append_bar();
assert_eq!(foo.pop().unwrap(), String::from("Bar"));
assert_eq!(foo.pop().unwrap(), String::from("Foo"));

即使变量 foo 被声明为可变的,方法 append_bar() 也会接收一个不可变的变量。您不需要借用 self 因为您不是要获得完全所有权,而是要修改驻留在所述变量中的现有数据。正确答案是

fn append_bar(mut self) -> Self {
self.push("Bar".to_owned()); // || .to_string() || String::from("Bar")
// Whatever gets the point across. As the String literal is essentially a "Borrowed" string.
self
}

append_bar() 的范围内,您正在尝试改变 String 的集合并返回它和附加的字符串。

关于RuSTLings 练习 Traits2,在 Vec 上实现 Trait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60775774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com