method #193
Replies: 33 comments 18 replies
-
请教一下,第5题里面,为什么如下写法也可以编译通过并执行成功呢? fn main() {
let light = TrafficLight::new();
assert_eq!(light.get_state(), "red");
} |
Beta Was this translation helpful? Give feedback.
-
最后一题应该返回字面值吧 #[derive(Debug)]
enum TrafficLightColor {
Red,
Yellow,
Green,
}
// 为 TrafficLightColor 实现所需的方法
impl TrafficLightColor {
fn color(&self) -> &str {
match *self {
TrafficLightColor::Red => "red",
TrafficLightColor::Yellow => "yellow",
TrafficLightColor::Green => "Green",
}
}
}
fn main() {
let c = TrafficLightColor::Yellow;
assert_eq!(c.color(), "yellow");
println!("{:?}",c);
} |
Beta Was this translation helpful? Give feedback.
-
fn color(&self) ->&str{ |
Beta Was this translation helpful? Give feedback.
-
impl TrafficLightColor { 这个能改成 if let 匹配吗? |
Beta Was this translation helpful? Give feedback.
-
为什么用
|
Beta Was this translation helpful? Give feedback.
-
1、方法的声明 struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
// 完成 area 方法,返回矩形 Rectangle 的面积
fn area(&self)->u32{
self.width*self.height
}
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
assert_eq!(rect1.area(), 1500);
} 2、方法中 // 只填空,不要删除任何代码行!
#[derive(Debug)]
struct TrafficLight {
color: String,
}
impl TrafficLight {
pub fn show_state(&self) {//不可以用self,会导致所有权转移
println!("the current state is {}", self.color);
}
}
fn main() {
let light = TrafficLight{
color: "red".to_owned(),
};
// 不要拿走 `light` 的所有权
light.show_state();
// 否则下面代码会报错
println!("{:?}", light);
} 3、方法中 struct TrafficLight {
color: String,
}
impl TrafficLight {
// 使用 `Self` 填空
pub fn show_state(&self) {
println!("the current state is {}", self.color);
}
// 填空,不要使用 `Self` 或其变体
pub fn change_state(&mut self) {
self.color = "green".to_string()
}
}
fn main() {} 4、 #[derive(Debug)]
struct TrafficLight {
color: String,
}
impl TrafficLight {
// 1. 实现下面的关联函数 `new`,
// 2. 该函数返回一个 TrafficLight 实例,包含 `color` "red"
// 3. 该函数必须使用 `Self` 作为类型,不能在签名或者函数体中使用 `TrafficLight`
pub fn new()->Self{
Self{
color:String::from("red"),
}
}
pub fn get_state(&self) -> &str {
&self.color
}
}
fn main() {
let light = TrafficLight::new();
//println!("{:?}",light);
assert_eq!(light.get_state(), "red");
} 5、每个结构体可以对应多个 struct Rectangle {
width: u32,
height: u32,
}
// 使用多个 `impl` 语句块重写下面的代码
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
impl Rectangle{
fn can_hold(&self, other: &Rectangle) -> bool {
self.width > other.width && self.height > other.height
}
}
fn main() {} 6、 //自己的答案,可以度过,但是很明显逻辑看上去很奇怪
#[derive(Debug)]
enum TrafficLightColor {
Red,
Yellow,
Green,
}
// 为 TrafficLightColor 实现所需的方法
impl TrafficLightColor {
pub fn color(&self)->&str{
"yellow"
}
}
fn main() {
let c = TrafficLightColor::Yellow;
assert_eq!(c.color(), "yellow");
println!("{:?}",c);
} 官方答案 #[derive(Debug)]
enum TrafficLightColor {
Red,
Yellow,
Green,
}
// implement TrafficLightColor with a method
impl TrafficLightColor {
fn color(&self) -> String {
//模式匹配赛高
match *self {
TrafficLightColor::Red => "red".to_string(),
TrafficLightColor::Yellow => "yellow".to_string(),
TrafficLightColor::Green => "green".to_string(),
}
}
}
fn main() {
let c = TrafficLightColor::Yellow;
assert_eq!(c.color(), "yellow");
println!("{:?}", c);
} |
Beta Was this translation helpful? Give feedback.
-
main.rs version 1
main.rs version2
Cargo.toml
I use MacOS version 12.6.7 and VSCode. Why there is the error? How can I fix it?
|
Beta Was this translation helpful? Give feedback.
-
不得不说比前面模式匹配简单,模式匹配最后那个所有权转移借用什么的问了ai、看了博客还是似懂非懂 |
Beta Was this translation helpful? Give feedback.
-
assert_eq!()函数可以同时比较String与&str这两个类型具体的内容是否相同。 |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
impl TrafficLightColor { |
Beta Was this translation helpful? Give feedback.
-
Done
|
Beta Was this translation helpful? Give feedback.
-
请教一下,为什么第二题用的是self.color?
两种方式我都试过了,都可以正常编译,使用答案中提示的self.color有什么好处? |
Beta Was this translation helpful? Give feedback.
-
method
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/method.html
Beta Was this translation helpful? Give feedback.
All reactions