Skip to content

generics ex2 #81016

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tariqjamil-bwp opened this issue Jan 14, 2021 · 0 comments
Closed

generics ex2 #81016

tariqjamil-bwp opened this issue Jan 14, 2021 · 0 comments

Comments

@tariqjamil-bwp
Copy link

struct Point <T, U> {
    x: T,
    y: U,
}

impl <T,U> Point <T,U> {
    fn mixup<V,W> (self, other:Point<V,W>) -> Point <T,W> {
        Point {
            x: self.x,
            y: other.y,
        }
    }
}

enum Option <T> {
    Some(T),
    None,
}

use std::fmt::Display;
fn main() {
    let p1 = Point {x:5, y:10.4};
    let p2 = Point {x: "Hello", y: 'c'};
    
    let p3 = p1.mixup(p2);
    
    println!("p3.x = {}, p3.y = {}", p3.x, p3.y);
    
    let integer = Option::Some(5);
    let float = Option::Some(5.0);
    
    println!("integer:{:?}, float:{:?}", integer, float);
    
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
warning: unused import: `std::fmt::Display`
  --> src/main.rs:20:5
   |
20 | use std::fmt::Display;
   |     ^^^^^^^^^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

error[E0277]: `Option<{integer}>` doesn't implement `Debug`
  --> src/main.rs:32:42
   |
32 |     println!("integer:{:?}, float:{:?}", integer, float);
   |                                          ^^^^^^^ `Option<{integer}>` cannot be formatted using `{:?}`
   |
   = help: the trait `Debug` is not implemented for `Option<{integer}>`
   = note: add `#[derive(Debug)]` or manually implement `Debug`
   = note: required by `std::fmt::Debug::fmt`
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: `Option<{float}>` doesn't implement `Debug`
  --> src/main.rs:32:51
   |
32 |     println!("integer:{:?}, float:{:?}", integer, float);
   |                                                   ^^^^^ `Option<{float}>` cannot be formatted using `{:?}`
   |
   = help: the trait `Debug` is not implemented for `Option<{float}>`
   = note: add `#[derive(Debug)]` or manually implement `Debug`
   = note: required by `std::fmt::Debug::fmt`
   = note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors; 1 warning emitted

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants