2017-02-21 83 views
2

我想使用泛型來計算因子,但在main中遇到錯誤。如何處理仿製藥?

我全碼:

pub trait Body { 
    fn new() -> Self; 

    fn fact(&self, x: usize) -> usize { 
     match x { 
      1 => 1, 
      _ => x * self.fact(x - 1), 
     } 
    } 
} 

#[derive(Clone, Debug)] 
pub struct RecursiveCall<T: Body> { 
    level: usize, 
    indicator: String, 
    n_repeat: usize, 
    body: T, 
} 

impl<T> RecursiveCall<T> 
    where T: Body 
{ 
    fn new(n_repeat: usize) -> RecursiveCall<T> { 
     RecursiveCall { 
      level: 0, 
      indicator: "- ".to_string(), 
      n_repeat: n_repeat, 
      body: <T as Body>::new(), 
     } 
    } 

    fn pre_trace(&self, fname: &str, arg: &usize) { 
     let args: String = arg.to_string(); 
     println!("{}", 
       (vec![self.indicator.as_str(); self.level]).join("") + 
       self.level.to_string().as_str() + ":" + fname + "(" + 
       args.as_str() + ")"); 
    } 

    fn post_trace(&self, fname: &str, arg: &usize, ret: &usize) { 
     println!("{}", 
       (vec![self.indicator.as_str(); self.level]).join("") + 
       self.level.to_string().as_str() + ":" + fname + "=" + 
       ret.to_string().as_str()); 
    } 

    fn print_trace(&mut self) { 
     &self.pre_trace("fact", &self.n_repeat); 
     self.level += 1; 
     let ret = &self.body.fact(self.n_repeat); 
     self.level -= 1; 
     &self.post_trace("fact", &self.n_repeat, ret); 

     println!("Difference={}", &ret.to_string().as_str()); 
    } 
} 

type B = Body; 
fn main() { 
    let t = RecursiveCall::<B>::new(); 
} 

這個錯誤發生在main()

error: no associated item named `new` found for type `RecursiveCall<Body + 'static>` in the current scope 
    --> src/main.rs:61:13 
    | 
61 |  let t = RecursiveCall::<B>::new(); 
    |    ^^^^^^^^^^^^^^^^^^^^^^^ 
    | 
    = note: the method `new` exists but the following trait bounds were not satisfied: `Body : std::marker::Sized`, `Body : Body` 
    = help: items from traits can only be used if the trait is implemented and in scope; the following traits define an item `new`, perhaps you need to implement one of them: 
    = help: candidate #1: `Body` 
    = help: candidate #2: `std::sys_common::thread_info::NewThread` 
    = help: candidate #3: `std::iter::ZipImpl` 

error[E0277]: the trait bound `Body + 'static: std::marker::Sized` is not satisfied 
    --> src/main.rs:61:13 
    | 
61 |  let t = RecursiveCall::<B>::new(); 
    |    ^^^^^^^^^^^^^^^^^^^^^^^ the trait `std::marker::Sized` is not implemented for `Body + 'static` 
    | 
    = note: `Body + 'static` does not have a constant size known at compile-time 
    = note: required by `RecursiveCall` 

error[E0277]: the trait bound `Body + 'static: Body` is not satisfied 
    --> src/main.rs:61:13 
    | 
61 |  let t = RecursiveCall::<B>::new(); 
    |    ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Body` is not implemented for `Body + 'static` 
    | 
    = note: required by `RecursiveCall` 

error[E0038]: the trait `Body` cannot be made into an object 
    --> src/main.rs:61:13 
    | 
61 |  let t = RecursiveCall::<B>::new(); 
    |    ^^^^^^^^^^^^^^^^^^^^^^^ the trait `Body` cannot be made into an object 
    | 
    = note: method `new` has no receiver 

回答

5

答案的關鍵是從編譯器這樣一個字條:

注意:方法new存在,但不滿足以下特徵邊界:Body : std::marker::Sized,Body : Body

您已經定義B作爲一個類型別名Body,所以名稱BBody都意味着同樣的事情在你的程序(至少在這個模塊)。

定義特徵時,編譯器還定義了一個具有相同名稱的類型。但是,該類型不能直接實例化(與C++/C#/ Java /等中的類不同)。然而,這正是你想要做的!

不符合性狀Body : std::marker::Sized,因爲Body是編譯器定義的對應於同名性狀的類型,是未定義類型。未定義類型只能用於指針和引用(例如,&Body,Box<Body>等)。

特質綁定Body : Body不滿意,因爲你的特質不是object-safe。它不是對象安全的,因爲方法new沒有self參數(這是編譯器在最後一個注意事項中的含義:method `new` has no receiver)。

通常情況下,您將定義一個結構或枚舉並實現該類型的特徵,然後在實例化RecursiveCall時使用該類型。嘗試用以下代碼替換type B = Body;

struct B; 

impl Body for B { 
    fn new() -> B { 
     B 
    } 
} 
+0

我通過您的幫助解決了問題! 謝謝你的好意。 –