nandi/rustnimpublic Fork 0
5fdd5be9905d7aab2a8941bef4846c2ffb79c765
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.

Add trait impls and slice iterators; base16ct's decoder now goes through ae9f986 · on 5fdd5be9905d7aab2a8941bef4846c2ffb79c765 · nandithebull · 17h ago
024-trait-impls.rs · 47 lines · 1.1 KBRust Blame HistoryRaw
 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());
}