トップ «前の日記 最新 次の日記»

2022-07-06 (We) [長年日記]

_ Rust 使ってみる

pub struct Tokenizer<'a> {
    config: Config,
    dict: JapaneseDictionary,
    analyzer: StatefulTokenizer<&'a JapaneseDictionary>,
}

impl<'a> Tokenizer<'a> {
    pub fn new() -> Tokenizer<'a> {
        let config = Config::new(
            Some(PathBuf::from("./t/sudachi.rs/resources/sudachi.json")),
            Some(PathBuf::from("./t/sudachi.rs/resources")),
            Some(PathBuf::from("./t/sudachi.rs/resources/system.dic")),
        ).expect("Failed to load config file");
        let dict = JapaneseDictionary::from_cfg(&config)
            .unwrap_or_else(|e| panic!("Failed to create dictionary: {:?}", e));
        let analyzer = StatefulTokenizer::new(&dict, Mode::A);
        Tokenizer {
            config,
            dict,
            analyzer,
        }
    }
}
error[E0515]: cannot return value referencing local variable `dict`
  --> src/main.rs:24:2
   |
23 |       let analyzer = StatefulTokenizer::new(&dict, Mode::A);
   |                                             ----- `dict` is borrowed here
24 | /     Tokenizer {
25 | |         config,
26 | |         dict,
27 | |         analyzer,
28 | |     }
   | |_____^ returns a value referencing data owned by the current function

error[E0505]: cannot move out of `dict` because it is borrowed
  --> src/main.rs:26:6
   |
14 |   impl<'a> Tokenizer<'a> {
   |        -- lifetime `'a` defined here
...
23 |       let analyzer = StatefulTokenizer::new(&dict, Mode::A);
   |                                             ----- borrow of `dict` occurs here
24 | /     Tokenizer {
25 | |         config,
26 | |         dict,
   | |         ^^^^ move out of `dict` occurs here
27 | |         analyzer,
28 | |     }
   | |_____- returning this value requires that `dict` is borrowed for `'a`

言わんとしてることは、なんとなくわかるんだけど、 どう解決するんだろう?

全部 main でやっちゃうことはできなくはないけど、 あんまりやりたくないなぁ。。


編集 パスワード変更