You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The `Debug` trait enables debug formatting in format strings, which you indicate by adding `:?` within `{}` placeholders.
38
38
39
-
The `Debug` trait allows you to print instances of a type for debugging purposes, so you and other programmers using your type can inspect an instance at a particular point in a program’s execution.
39
+
It allows you to print instances of a type for debugging purposes, so you and other programmers using this type can inspect an instance at a particular point in a program’s execution.
40
40
41
-
For example:
41
+
For example, if you want to print the value of a variable of type `Point`, you can do it as follows:
The `Debug` trait is required, forexample,in use of the `assert_eq!`macroin tests. This macro prints the values of instances given as arguments if the equality assertion fails so programmers can see why the two instances weren’t equal.
52
+
The `Debug` trait is required, forexample, when using the `assert_xx!` macrosin tests. Theses macros print the values of instances given as arguments if the equality or comparison assertion fails so programmers can see why the two instances weren’t equal.
Copy file name to clipboardExpand all lines: src/ch11-09-printing.md
+33-9Lines changed: 33 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
3
3
When writing a program, it is quite common to print some data to the console, either for the normal process of the program or for debugging purpose. In this chapter, we describe the options you have to print simple and complex data types.
4
4
5
-
## Printing standard data types
5
+
## Printing Standard Data Types
6
6
7
7
Cairo provides two macros to print standard data types:
> `print!` and `println!` macros use the `Display` trait under the hood, and are therefore used to print the value of types that implement it. This is the case for basic data types, but not for more complexe ones. If you try to print complex data types values with these macros, e.g., for debugging purpose, you will get an error. In that case, you can either [manually implement](./ch11-09-printing.md#printing-custom-data-types) the `Display` trait for your type, or use the `Debug` trait (see [below](./ch11-09-printing.md#print-debug-traces)).
26
+
25
27
## Formatting
26
28
27
29
Cairo also provides a useful macro to handle strings formatting: `format!`. This macro works like `println!`, but instead of printing the output to the screen, it returns a `ByteArray` with the contents. In the following example, we perform string concatenation using either the `+` operator or the
@@ -31,19 +33,33 @@ Cairo also provides a useful macro to handle strings formatting: `format!`. This
As previously explained, if you try to print the value of a custom data type with `print!` or `println!` macros, you'll get an error telling you that the `Display` trait is not implemented for your custom type:
39
+
40
+
```shell
41
+
error: Trait has no implementation in context: core::fmt::Display::<package_name::struct_name>
42
+
```
35
43
36
-
If you try to print a custom data type with `print!` and `println!`macros, you'll get an error telling you that the `Display`trait is not implemented for your custom type.
44
+
The `println!` macro can do many kinds of formatting, and by default, the curly brackets tell `println!` to use formatting known as `Display`: output intended for direct end user consumption. The primitive types we’ve seen so far implement `Display` by default because there’s only one way you’d want to show a `1` or any other primitive type to a user. But with structs, the way `println!`should format the output is less clear because there are more display possibilities: Do you want commas or not? Do you want to print the curly brackets? Should all the fields be shown? Due to this ambiguity, Cairo doesn’t try to guess what we want, and structs don’t have a provided implementation of `Display`to use with `println!` and the `{}` placeholder.
> Printing complex data types this way might not be ideal as it requires additional steps to allow the use of `print!` and `println!` macros. If you need to print complexe data types, especially when debugging, use the `Debug` trait described just after instead.
84
+
85
+
## Print Debug Traces
86
+
87
+
Cairo provides the derivable trait `Debug` to print the value of variables when debugging. Simply add `:?` within curly brackets `{}` placeholders in a `print!` or `println!` macro string input.
88
+
89
+
This trait is very useful and is implemented by default for basic data types. It can also be simply derived on complex data types using the `derive(Debug)` attribute, as long as all types they contain implement it. It allows to get rid of manually implementing extra-code to print complex data types values.
90
+
91
+
Note that `assert_xx!` macros used in tests require the provided values to implement the `Debug` trait, as they also print the result in case of assertion failure.
68
92
69
-
To print some debug data, Cairo provides the derivable trait `Debug`. Look at the [Derivable Traits](appendix-03-derivable-traits.md) appendix, for more details.
93
+
Please refer to the [Derivable Traits](appendix-03-derivable-traits.md#debug-trait-for-printing-and-debugging) appendix for more detail about the `Debug` trait and its usage for printing value when debugging.
0 commit comments