Derivare Debug in Rust
Livello 3115 battiture
#[derive(Debug)] fa sì che il compilatore implementi automaticamente il trait Debug, così la struct si può stampare con {:?}.
Codice Rust
// Chiede al compilatore di generare un'implementazione di Debug per il tipo seguente1#[derive(Debug)]// Una struct con due campi i32, x e y2struct Point {3 x: i32,4 y: i32,5}6 7fn main() { // Crea un Point con x = 3 e y = 48 let p = Point { x: 3, y: 4 }; // {:?} usa il formato derivato: Point { x: 3, y: 4 }9 println!("{:?}", p);10}Output
Point { x: 3, y: 4 }Esercitati a digitare questo codice. I risultati non vengono registrati in classifica.
Simboli in questo codice
I simboli che digiti in questo codice Rust e quante volte compaiono.
:× 5,× 4{× 4}× 4(× 3)× 3;× 2"× 2!× 1?× 1