compound-types/struct #169
Replies: 40 comments 6 replies
-
struct Color(i32, i32, i32); fn check_color(p: Point) { check_color()中,let Point(x, _, _) = p; 这一句发生了所有权转移吗? |
Beta Was this translation helpful? Give feedback.
-
Done,对于结构体的所有权设计我并不是很能理解结构体的所有权和他的属性的所有权是分开的。这样做的优点是什么? |
Beta Was this translation helpful? Give feedback.
-
变量绑定时所有权是否移动,可不可以理解为,只要类型实现了Copy trait,就自动拷贝,所有权不移动,原变量可以继续使用,如果没有实现则发生所有权移动,原变量不可以使用? |
Beta Was this translation helpful? Give feedback.
-
Done. 第8道有点想不到既然是这样就可以了 |
Beta Was this translation helpful? Give feedback.
-
2022.11.14 Done |
Beta Was this translation helpful? Give feedback.
-
结构体还有一个存在,struct FakeUnit {} |
Beta Was this translation helpful? Give feedback.
-
Done.第三题卡了一下,没想到如何解构出Color内的字段值,看了几次https://course.rs/basic/compound-type/struct.html#%E5%85%83%E7%BB%84%E7%BB%93%E6%9E%84%E4%BD%93tuple-struct 也没理解,在后面看到了 let Peron {name,age} 这个声明,想到应该也是一样的吧。概念是真的多=_= |
Beta Was this translation helpful? Give feedback.
-
这写起来确实限制很多,从java看过来... |
Beta Was this translation helpful? Give feedback.
-
个人建议3题是不是将要求改为 :修复错误并填空 好些,原有的填空并修复错误。 容易理解为 仅用填空的方式就能修复错误。 |
Beta Was this translation helpful? Give feedback.
-
fn main() {
// 1.
// let age = 30;
// let p = Person {
// name: String::from("Baker"),
// age: age,
// hobby: String::from("reading")
// };
// 2.
// let u = Unit;
// do_something_with_unit(u);
// 3.
// let v = Color(0, 127, 255);
// check_color(v);
// 4.
// let age = 18;
// let mut p = Person {
// name: String::from("Baker"),
// age: age,
// };
// p.age = 20;
// p.name = String::from("Jack");
// 5.
// 6.
// let u1 = User {
// email: String::from("[email protected]"),
// username: String::from("sunface"),
// active: true,
// sign_in_count: 1,
// };
// let u2 = set_email(u1);
// 7.
// let scale = 2;
// let rect1 = Rectangle {
// width: dbg!(30 * scale),
// height: 50,
// };
// dbg!(&rect1);
// println!("rect1 is {:?}", rect1);
// 8.
let f = File {
name: String::from("readme.txt"),
data: "Rust By Practice".to_string(),
};
let _name = f.name;
println!("{}, {}", _name, f.data);
// println!("Hello, world!");
}
// 1.
// struct Person {
// name: String,
// age: u8,
// hobby:String
// }
// 2.
// struct Unit;
// trait SomeTrait {}
// impl SomeTrait for Unit {}
// fn do_something_with_unit(u: Unit) {
// println!("do something with unit");
// }
// 3.
// struct Color(i32, i32, i32);
// struct Point(i32, i32, i32);
// fn check_color(p: Color) {
// // let (x, y, z) = p;
// let x = p.0;
// let z = p.2;
// assert_eq!(x, 0);
// assert_eq!(p.1, 127);
// assert_eq!(z, 255);
// }
// 4.
// struct Person {
// name: String,
// age: u8,
// }
// 5.
// struct Person {
// name: String,
// age: u8,
// }
// fn build_person(name: String, age: u8) -> Person {
// Person {
// name,
// age,
// }
// }
// 6.
// struct User {
// active: bool,
// username: String,
// email: String,
// sign_in_count: u64,
// }
// fn set_email(u: User) -> User{
// User {
// email: String::from("[email protected]"),
// ..u
// }
// }
// 7.
// #[derive(Debug)]
// struct Rectangle {
// width: u32,
// height: u32,
// }
// 8.
#[derive(Debug)]
struct File {
name: String,
data: String,
} |
Beta Was this translation helpful? Give feedback.
-
Done. 被第三题卡了,没看仔细,将_看成__了,在疑惑(x, _, _)两个_填什么0.o,哈哈🤣 |
Beta Was this translation helpful? Give feedback.
-
mark finished |
Beta Was this translation helpful? Give feedback.
-
// Fix the error and fill the blanks
} fn check_color(p: Color) { Answer` // Fix the error and fill the blanks
} fn check_color(p: Color) { |
Beta Was this translation helpful? Give feedback.
-
success(8/8) |
Beta Was this translation helpful? Give feedback.
-
compound-types/struct
Learning Rust By Practice, narrowing the gap between beginner and skilled-dev with challenging examples, exercises and projects.
https://zh.practice.rs/compound-types/struct.html
Beta Was this translation helpful? Give feedback.
All reactions