rust - How to call a trait method without a struct instance? -


if have struct method doesn't have self argument, can call method via somestruct::method(). can't seem same method that's defined trait. example:

trait sometrait {     fn one_trait() -> uint; }  struct somestruct; impl somestruct {     fn one_notrait() -> uint {         1u     } } impl sometrait somestruct {     fn one_trait() -> uint {         1u     } }  #[test] fn testing() {     somestruct::one_trait();   // doesn't compile     somestruct::one_notrait(); // compiles } 

the compiler gives error "unresolved name 'somestruct::one_trait.'"

how can call struct's implementation of trait method directly?

i believe not possible. problem cannot explicitly specify self type. there active rfc in pipeline should allow when implemented.

in meantime, work around this:

trait sometrait {     fn one_trait(&self) -> uint; }  struct static<t>;  struct somestruct;  impl sometrait static<somestruct> {     fn one_trait(&self) -> uint { 1 } }  fn main() {     let type_to_uint = static::<somestruct>.one_trait();     println!("{}", type_to_uint); } 

this how map type integer (if that's you're after). it's done without having value of type t. dummy value, static<t>, has size of zero.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -