Problem with traits during learning rust


#1

Hello!
I’ve got an unexpected (for me) problem while trying to learn rust. I’ve created two files:
shape.rs:

trait Shape {
fn where_am_i(&self) {
println!(“Shape!”);
}
}

pub struct Rect {
ul_x : i16,
ul_y : i16,
lr_x : i16,
lr_y : i16
}

impl Shape for Rect {
fn where_am_i(&self) {
println!(“Rect!”);
}
}

and main.rs:

mod shape;

use shape::*;

fn main() {
let r : Rect;
r.where_am_i();
}

during compiling i’ve got an error E0599: no method named ‘where_am_i’ found for type ‘shape::Rect’ in the current scope
r.where_am_i();
help: items from traits can only be used if the trait is implemented and in scope
note: the following trait defines an item ‘where_am_i’, perhaps you need to implement it
and so on…

Please, point me my error and please, don’t be cruel, cause it’s my second rust program after “Hello World”))
Thanks in advance.


#2

Your Shape trait is private, so use shape::* doesn’t bring it into scope, and the compiler complains. (With a non-wildcard use that would’ve been immediately obvious, which is why I avoid wildcards as a rule.)

When you make the trait public, you’ll get the “possibly uninitialized” error, which is to be expected.


#3

Thanks! That helped!