Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use std::ops::Deref;
- use std::fmt::Display;
- trait Test {
- fn test(&self) -> String;
- }
- #[derive(Default)]
- struct Child {
- a: f32,
- b: f32,
- }
- impl Test for Child {
- fn test(&self) -> String {
- return String::from("test");
- }
- }
- impl std::fmt::Display for Child {
- fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
- // The `f` value implements the `Write` trait, which is what the
- // write! macro is expecting. Note that this formatting ignores the
- // various flags provided to format strings.
- write!(f, "({}, {})", self.a, self.b)
- }
- }
- #[derive(Default)]
- struct Parent {
- ch: Child,
- c: f32,
- d: f32,
- }
- impl Deref for Parent {
- type Target = Child;
- fn deref(&self) -> &Child {
- &self.ch
- }
- }
- fn main() {
- let p = Parent{
- ch: Child {
- a: 1.0,
- b: 2.0,
- },
- c: 3.0,
- d: 4.0,
- };
- println!("{}", p.a);
- println!("{}", p.b);
- println!("{}", p.c);
- println!("{}", p.d);
- println!("{}", p.test());
- println!("{}", *p);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement