compound-types/struct #169
Replies: 32 comments 7 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.
-
1、对于结构体,我们必须为其中的每一个字段都指定具体的值 Q: // fix the error
struct Person {
name: String,
age: u8,
hobby: String
}
fn main() {
let age = 30;
let p = Person {
name: String::from("sunface"),
age,
};
} A: // fix the error
struct Person {
name: String,
age: u8,
hobby: String
}
fn main() {
let age = 30;
let p = Person {
name: String::from("sunface"),
age:1,
hobby:String::from("draw"),
};
} 2、 Q: struct Unit;
trait SomeTrait {
// ...定义一些行为
}
// 我们并不关心结构体中有什么数据( 字段 ),但我们关心它的行为。
// 因此这里我们使用没有任何字段的单元结构体,然后为它实现一些行为
impl SomeTrait for Unit { }
fn main() {
let u = Unit;
do_something_with_unit(u);
}
// 填空,让代码工作
fn do_something_with_unit(u: __) { } A: struct Unit;
trait SomeTrait {
// ...定义一些行为
}
// 我们并不关心结构体中有什么数据( 字段 ),但我们关心它的行为。
// 因此这里我们使用没有任何字段的单元结构体,然后为它实现一些行为
impl SomeTrait for Unit { }
fn main() {
let u = Unit;
do_something_with_unit(u);
}
fn do_something_with_unit(u: Unit) { } 3、 Q: // 填空并修复错误
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
fn main() {
let v = Point(__, __, __);
check_color(v);
}
fn check_color(p: Color) {
let (x, _, _) = p;
assert_eq!(x, 0);
assert_eq!(p.1, 127);
assert_eq!(__, 255);
} A: struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
fn main() {
let v = Color(0, 127, 255);//这里需要和调用函数的参数一致
check_color(v);
}
fn check_color(p: Color) {
let Color(x, y, z) = p;//需要声明这是元组结构体而不是元组
assert_eq!(x, 0);
assert_eq!(p.1, 127);
assert_eq!(z, 255);
} 4、结构体整体可变或者整体不可变,不可单独对结构体内容中进行mut声明 Q: // 填空并修复错误,不要增加或移除代码行
struct Person {
name: String,
age: u8,
}
fn main() {
let age = 18;
let p = Person {
name: String::from("sunface"),
age,
};
// how can you believe sunface is only 18?
p.age = 30;
// 填空
__ = String::from("sunfei");
} A: // 填空并修复错误,不要增加或移除代码行
struct Person {
name: String,
age: u8,
}
fn main() {
let age = 18;
let mut p = Person {//可变
name: String::from("sunface"),
age:age,//需要全部初始化
};
// how can you believe sunface is only 18?
p.age = 30;
// 填空
p.name = String::from("sunfei");
} 5、使用结构体字段初始化缩略语法可以减少一些重复代码 A: // 填空
struct Person {
name: String,
age: u8,
}
fn main() {}
fn build_person(name: String, age: u8) -> Person {
Person {
age,
name,
}
} 6、你可以使用结构体更新语法基于一个结构体实例来构造另一个 Q: // 填空,让代码工作
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn main() {
let u1 = User {
email: String::from("[email protected]"),
username: String::from("sunface"),
active: true,
sign_in_count: 1,
};
let u2 = set_email(u1);
}
fn set_email(u: User) -> User {
User {
email: String::from("[email protected]"),
__
}
} A: // 填空,让代码工作
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn main() {
let u1 = User {
email: String::from("[email protected]"),
username: String::from("sunface"),
active: true,
sign_in_count: 1,
};
let u2 = set_email(u1);
}
fn set_email(u: User) -> User {
User {
email: String::from("[email protected]"),
..u//直接剩下的部分照搬u
}
} 7、#[derive(Debug)]来使得结构体可以打印 Q: // 填空,让代码工作
#[__]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale), // 打印 debug 信息到标准错误输出 stderr,并将 `30 * scale` 的值赋给 `width`
height: 50,
};
dbg!(&rect1); // 打印 debug 信息到标准错误输出 stderr
println!(__, rect1); // 打印 debug 信息到标准输出 stdout
} A: // 填空,让代码工作
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
fn main() {
let scale = 2;
let rect1 = Rectangle {
width: dbg!(30 * scale), // 打印 debug 信息到标准错误输出 stderr,并将 `30 * scale` 的值赋给 `width`
height: 50,
};
dbg!(&rect1); // 打印 debug 信息到标准错误输出 stderr
println!("{:#?}", rect1); // 打印 debug 信息到标准输出 stdout
} 8、修复错误:所有权 Q: // 修复错误
#[derive(Debug)]
struct File {
name: String,
data: String,
}
fn main() {
let f = File {
name: String::from("readme.md"),
data: "Rust By Practice".to_string()
};
let _name = f.name;
// 只能修改这一行
println!("{}, {}, {:?}",f.name, f.data, f);
} A: // 修复错误
#[derive(Debug)]
struct File {
name: String,
data: String,
}
fn main() {
let f = File {
name: String::from("readme.md"),
data: "Rust By Practice".to_string()
};
let _name = f.name;//这一步让f和f.name的所有权丢掉了,但是f.data还在
println!("{}, {}",_name, f.data);
} |
Beta Was this translation helpful? Give feedback.
-
Done!顺便想问一下:
圣经里面是这样说的,但是在Q4当中出现了这样的写法: let age = 18;
let p = Person {
name: String::from("sunface"),
age,
}; 是否意味着其实这个简化写法是不只仅限于函数参数,变量名就可以? |
Beta Was this translation helpful? Give feedback.
-
done--2023/12/11 |
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.
-
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