2017-11-12 7 views
0

私はlib.rsに次の文脈で、「借用したコンテンツのライフタイムの参照残存期間」を解決するにはどうすればよいですか?

pub enum ConfigurationSource { 
    StringContent(String), 
    FileContent(PathBuf)  
} 

pub struct ConfigurationBuilder<'a> { 
    config: Value, 
    bundles: HashMap<&'a str, &'a Vec<ConfigurationSource>>  
} 

impl<'a> ConfigurationBuilder<'a>{ 
    pub fn new(base_source: &ConfigurationSource) -> ConfigurationBuilder{ 
     let base_config: Value = from_str("{}").unwrap(); 

     let mut config_builder = ConfigurationBuilder{ 
      config: base_config, 
      bundles: HashMap::new() 
     }; 

     config_builder.merge_source(&base_source); 

     return config_builder; 
    } 

    //more code here 

    pub fn define_bundle(&mut self, bundle_key: &str, sources: &Vec<ConfigurationSource>){ 
     self.bundles.insert(bundle_key, sources); 
    } 
} 

を、以下の構造体を持っている私はbundle_keyのかdefine_bundleメソッドに渡されたsource Sを所有するConfigurationBuilderインスタンスでバンドルをHashMapの必要はありません。

私は、次の2つは、私が間違って何をやっている

error[E0312]: lifetime of reference outlives lifetime of borrowed content... 
    --> src\lib.rs:67:41 
    | 
67 |   self.bundles.insert(bundle_key, sources); 
    |           ^^^^^^^ 
    | 
note: ...the reference is valid for the lifetime 'a as defined on the impl at 27:1... 
    --> src\lib.rs:27:1 
    | 
27 |/impl<'a> ConfigurationBuilder<'a>{ 
28 | | 
29 | |  pub fn new(base_source: &ConfigurationSource) -> ConfigurationBuilder{ 
30 | |   let base_config: Value = from_str("{}").unwrap(); 
... | 
89 | |  } 
90 | | } 
    | |_^ 
note: ...but the borrowed content is only valid for the anonymous lifetime #3 defined on the method 
body at 66:5 
    --> src\lib.rs:66:5 
    | 
66 |/ pub fn define_bundle(&mut self, bundle_key: &str, sources: &Vec<ConfigurationSource>){ 
67 | |   self.bundles.insert(bundle_key, sources); 
68 | |  } 
    | |_____^ 

error[E0312]: lifetime of reference outlives lifetime of borrowed content... 
    --> src\lib.rs:67:29 
    | 
67 |   self.bundles.insert(bundle_key, sources); 
    |        ^^^^^^^^^^ 
    | 
note: ...the reference is valid for the lifetime 'a as defined on the impl at 27:1... 
    --> src\lib.rs:27:1 
    | 
27 |/impl<'a> ConfigurationBuilder<'a>{ 
28 | | 
29 | |  pub fn new(base_source: &ConfigurationSource) -> ConfigurationBuilder{ 
30 | |   let base_config: Value = from_str("{}").unwrap(); 
... | 
89 | |  } 
90 | | } 
    | |_^ 
note: ...but the borrowed content is only valid for the anonymous lifetime #2 defined on the method 
body at 66:5 
    --> src\lib.rs:66:5 
    | 
66 |/ pub fn define_bundle(&mut self, bundle_key: &str, sources: &Vec<ConfigurationSource>){ 
67 | |   self.bundles.insert(bundle_key, sources); 
68 | |  } 
    | |_____^ 

ビルド時にコンパイルエラーを取得しますか?

答えて

2

define_bundleの入力パラメータは、コンパイラが知ることができない寿命を持つか、または構造体に定義されている寿命よりも長く存続します。

pub fn define_bundle(&mut self, bundle_key: &'a str, sources: &'a Vec<ConfigurationSource>) { 
    //          ^^----------------^^ same lifetimes as the struct fields expect 
    self.bundles.insert(bundle_key, sources); 
} 
:彼らは同じ寿命はトリックを行います期待コンパイラに伝える

関連する問題