Learn Rust Programming by Matzinger Claus;

Learn Rust Programming by Matzinger Claus;

Author:Matzinger, Claus; [Matzinger, Claus]
Language: eng
Format: epub, pdf
Publisher: BPB Publications
Published: 2022-03-15T00:00:00+00:00


Printing custom types

If you create your own type, there is no default formatting implemented, and any attempt to print it will fail with a compiler error. Even if all members of your type are standard types (for example, usize or String), there will not be any default way of printing the type.

That is unless you derive the Debug trait implementation. We will discuss the derive macros in Chapter 11, Generating Code with Macros; for now, just know that a #[derive(Debug)] statement automatically implements the Debug trait by recursively printing each member (this also works for other traits). Here is an example:

#[derive(Debug)]

struct Account {

name: String,

password: [u8; 5] // use 32 for a 256 bit hash (SHA-256)

}



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.