nandi/rustnimpublic Fork 0
0e6c394efb2da64065352ff67a15f6cba4e8899a
Commits
Clone
git clone https://git.rickub.com/nandi/rustnim.git
git clone ssh://git@rickub.com/nandi/rustnim.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

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