rust-by-practice/solutions/method.md

150 lines
2.4 KiB
Markdown
Raw Permalink Normal View History

2022-03-03 02:02:31 -06:00
1.
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
```rust
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn main() {
let rect1 = Rectangle { width: 30, height: 50 };
assert_eq!(rect1.area(), 1500);
}
```
2.
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
```rust
#[derive(Debug)]
struct TrafficLight {
color: String,
}
impl TrafficLight {
2022-05-01 00:08:50 -05:00
pub fn show_state(&self) {
2022-03-03 02:02:31 -06:00
println!("the current state is {}", self.color);
}
}
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
fn main() {
2022-05-01 00:08:50 -05:00
let light = TrafficLight {
2022-03-03 02:02:31 -06:00
color: "red".to_owned(),
};
// Don't take the ownership of `light` here
light.show_state();
// ..otherwise, there will be an error below
println!("{:?}", light);
}
```
3.
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
```rust
struct TrafficLight {
color: String,
}
impl TrafficLight {
// using `Self` to fill in the blank
2022-05-01 00:08:50 -05:00
pub fn show_state(self: &Self) {
2022-03-03 02:02:31 -06:00
println!("the current state is {}", self.color);
}
// fill in the blank, DON'T use any variants of `Self`
pub fn change_state(&mut self) {
self.color = "green".to_string()
}
}
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
fn main() {}
```
4.
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
```rust
#[derive(Debug)]
struct TrafficLight {
color: String,
}
impl TrafficLight {
// 1. implement a assotiated function `new`,
// 2. it will return a TrafficLight contains color "red"
// 3. must use `Self`, DONT use `TrafficLight`
pub fn new() -> Self {
Self {
color: "red".to_string()
}
}
pub fn get_state(&self) -> &str {
&self.color
}
}
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
fn main() {
let light = TrafficLight::new();
assert_eq!(light.get_state(), "red");
}
```
5.
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
```rust
struct Rectangle {
width: u32,
height: u32,
}
// rewrite Rectangle to use multiple `impl` blocks
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
}
}
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
fn main() {}
```
6.
2022-05-01 00:08:50 -05:00
2022-03-03 02:02:31 -06:00
```rust
#[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");
2022-05-01 00:08:50 -05:00
println!("{:?}", c);
2022-03-03 02:02:31 -06:00
}
```