1
0
mirror of https://github.com/google/comprehensive-rust.git synced 2025-07-14 18:14:29 +02:00

Use Self to create struct in method chapter (#1559)

If I'm not mistaken, it's idomatic to use `Self` in a struct constructor
rather than the name of the struct.
This commit is contained in:
Martin Huschenbett
2023-12-06 15:08:56 +01:00
committed by GitHub
parent 6c5061bb90
commit 70efef4f38

View File

@ -16,7 +16,7 @@ struct Race {
impl Race { impl Race {
fn new(name: &str) -> Self { // No receiver, a static method fn new(name: &str) -> Self { // No receiver, a static method
Race { name: String::from(name), laps: Vec::new() } Self { name: String::from(name), laps: Vec::new() }
} }
fn add_lap(&mut self, lap: i32) { // Exclusive borrowed read-write access to self fn add_lap(&mut self, lap: i32) { // Exclusive borrowed read-write access to self