LoginSignup
2
1

More than 3 years have passed since last update.

既存の Rust(2015) プロジェクトの Rust 2018 移行に伴う対応

Posted at

rssss を Rust 2018 対応した備忘。

Cargo.toml に edition = "2018" の指定を追加

diff --git a/backend/Cargo.toml b/backend/Cargo.toml
index 9b330a4..bf555a8 100644
--- a/backend/Cargo.toml
+++ b/backend/Cargo.toml
@@ -2,6 +2,7 @@
 name = "rssss"
 version = "0.1.0"
 authors = ["zaneli <shun.otani@gmail.com>"]
+edition = "2018"

 [dependencies]
 actix = "0.7"

rustc の更新

一旦 cargo run してみる。

> cargo run
   Compiling rssss v0.1.0 (/Users/zaneli/ws/github.com/zaneli-sandbox/rssss/backend)
error: Edition 2018 is unstable and only available for nightly builds of rustc.

手元の rustc が古い? :thinking:

> rustc --version
rustc 1.30.0 (da5f414c2 2018-10-24)

rustup で rustc を更新してみる。

rustup update
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2019-04-25, rust version 1.34.1 (fc50f328b 2019-04-24)
info: downloading component 'rustc'
 78.9 MiB /  78.9 MiB (100 %)   1.9 MiB/s ETA:   0 s
info: downloading component 'rust-std'
 51.1 MiB /  51.1 MiB (100 %)   1.8 MiB/s ETA:   0 s
info: downloading component 'cargo'
  3.4 MiB /   3.4 MiB (100 %)   2.0 MiB/s ETA:   0 s
info: downloading component 'rust-docs'
 10.2 MiB /  10.2 MiB (100 %)   2.5 MiB/s ETA:   0 s
info: downloading component 'rustfmt-preview'
info: downloading component 'rust-analysis'
info: downloading component 'rls-preview'
  5.8 MiB /   5.8 MiB (100 %)   2.9 MiB/s ETA:   0 s
info: downloading component 'rust-src'
info: removing component 'rustc'
info: removing component 'rust-std'
info: removing component 'cargo'
info: removing component 'rust-docs'
info: removing component 'rustfmt-preview'
info: removing component 'rust-analysis'
info: removing component 'rls-preview'
info: removing component 'rust-src'
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: installing component 'rustfmt-preview'
info: installing component 'rust-analysis'
info: installing component 'rls-preview'
info: installing component 'rust-src'
info: checking for self-updates
info: downloading self-update

  stable-x86_64-apple-darwin updated - rustc 1.34.1 (fc50f328b 2019-04-24)

再度バージョン確認。

> rustc --version
rustc 1.34.1 (fc50f328b 2019-04-24)

再度 cargo run。別のエラーが出る事を確認。

error: expected one of `:` or `@`, found `,`
   --> src/rss.rs:114:48
    |
114 |     fn parse_start_element(&mut self, OwnedName, Vec<OwnedAttribute>);
    |                                       ---------^ expected one of `:` or `@` here
    |                                       |
    |                                       help: explicitly ignore parameter: `_: OwnedName`
    |
    = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)

error: expected one of `:` or `@`, found `)`
   --> src/rss.rs:115:39
    |
115 |     fn parse_content(&mut self, String);
    |                                 ------^ expected one of `:` or `@` here
    |                                 |
    |                                 help: explicitly ignore parameter: `_: String`
    |
    = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)

error: expected one of `:` or `@`, found `)`
   --> src/rss.rs:116:46
    |
116 |     fn parse_end_element(&mut self, OwnedName);
    |                                     ---------^ expected one of `:` or `@` here
    |                                     |
    |                                     help: explicitly ignore parameter: `_: OwnedName`
    |
    = note: anonymous parameters are removed in the 2018 edition (see RFC 1685)

error[E0432]: unresolved import `error`
 --> src/rss.rs:2:5
  |
2 | use error::{Error, InvalidRssError};
  |     ^^^^^ did you mean `crate::error`?
  |
  = note: `use` statements changed in Rust 2018; read more at <https://doc.rust-lang.org/edition-guide/rust-2018/module-system/path-clarity.html>

(略)

error: aborting due to 16 previous errors

コンパイルエラー対応

trait 引数の変数名が省略不可?になったようなので対応。

diff --git a/backend/src/rss.rs b/backend/src/rss.rs
index 8151425..1680886 100644
--- a/backend/src/rss.rs
+++ b/backend/src/rss.rs
@@ -111,9 +111,9 @@ fn parse(buf: &bytes::Bytes, parser: &mut RssParser) -> Result<Vec<Rss>, Error>
 }
 trait RssParser {
-    fn parse_start_element(&mut self, OwnedName, Vec<OwnedAttribute>);
-    fn parse_content(&mut self, String);
-    fn parse_end_element(&mut self, OwnedName);
+    fn parse_start_element(&mut self, _: OwnedName, _: Vec<OwnedAttribute>);
+    fn parse_content(&mut self, _: String);
+    fn parse_end_element(&mut self, _: OwnedName);
     fn verify_rss(&self) -> Result<(), Error>;
     fn get_results(&self) -> Vec<Rss>;
 }

この時点でコンパイルが通る事を確認。

extern crate#[macro_use] を削除し、 use を使用

main.rss に書いていた extern crate を一律削除。
#[macro_use]extern crate を書いていたものはそのままではコンパイルが通らなくなるので、 use を追記。

#[macro_use]
extern crate log;
#[macro_use]
extern crate serde_derive;

:arrow_down:

use log::info;
use serde_derive::{Deserialize, Serialize};

以上。
差分はこの通り

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1