display trait rust
use std::fmt;
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)
}
}
let origin = Point { x: 0, y: 0 };
assert_eq!(format!("The origin is: {}", origin), "The origin is: (0, 0)");Run
Are there any code examples left?
New code examples in category Rust
-
Rust 2022-03-28 00:00:04 rust print i8
-
Rust 2022-03-27 16:15:02 rust check if key in hashmap
-
Rust 2022-03-22 14:45:18 rust convert a string (with decimals) to a floating point number.
-
Rust 2022-03-11 21:00:15 creating constants in rust
-
Rust 2022-03-07 02:55:02 hello world in rust
-
Rust 2022-03-03 21:10:04 levenshtein distance
-
Rust 2022-02-24 13:40:24 execution duration
-
Rust 2022-02-20 06:45:02 matching in rust
-
Rust 2022-02-19 18:35:12 rust bitset stack
-
Rust 2022-01-30 19:30:40 rust compile and run