1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
// A `Display` impl becomes a proc returning the string the formatter is
// written with, since the observable result of `{}` is exactly those bytes.
use core::fmt;
#[derive(Debug, PartialEq)]
pub enum Error {
InvalidEncoding,
InvalidLength,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidEncoding => f.write_str("invalid Base16 encoding"),
Error::InvalidLength => f.write_str("invalid Base16 length"),
}
}
}
// A marker trait with no items: we do not model trait resolution, so it
// generates nothing.
impl core::error::Error for Error {}
struct Point {
x: i32,
y: i32,
}
impl fmt::Display for Point {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
impl Point {
fn sum(&self) -> i32 {
self.x + self.y
}
}
fn main() {
println!("{}", Error::InvalidEncoding);
println!("{}", Error::InvalidLength);
println!("{:?}", Error::InvalidLength);
let p = Point { x: 3, y: -4 };
println!("{} {}", p, p.sum());
}
|