1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

wslでrust その4

Last updated at Posted at 2024-10-29

概要

wslでrustやってみた。
systemにjsonパーサーが、無いらしい。
systemにhttpクライエントが、無いらしい。
serdeクレートを呼びつけて、使ってみた。
reqwestクレートを呼びつけて、使ってみた。
郵便番号検索APIやってみた。

参考にしたページ

操作手順

~$ cd rust
~/rust$ ls
dentaku  hello  hello.rs  hello0  openmind  zundoko

~/rust$ cargo new zipapi
	 Created binary (application) `zipapi` package

~/rust$ cd zipapi

~/rust/zipapi$ cargo add reqwest
	Updating crates.io index
	  Adding reqwest v0.12.9 to dependencies.
			 Features:
			 + __tls
			 + charset
			 + default-tls
			 + h2
			 + http2
			 + macos-system-configuration
			 27 deactivated features
	Updating crates.io index
 
~/rust/zipapi$ cargo add tokio
	Updating crates.io index
	  Adding tokio v1.41.0 to dependencies.
			 Features:
			 - bytes
			 - fs
			 - full
			 - io-std
			 - io-util
			 - libc
			 - macros
			 - mio
			 - net
			 - parking_lot
			 - process
			 - rt
			 - rt-multi-thread
			 - signal
			 - signal-hook-registry
			 - socket2
			 - sync
			 - test-util
			 - time
			 - tokio-macros
			 - tracing
			 - windows-sys

~/rust/zipapi$ cargo add serde
	Updating crates.io index
	  Adding serde v1.0.214 to dependencies.
			 Features:
			 + std
			 - alloc
			 - derive
			 - rc
			 - serde_derive
			 - unstable

~/rust/zipapi$ cargo add serde_json
	Updating crates.io index
	  Adding serde_json v1.0.132 to dependencies.
			 Features:
			 + std
			 - alloc
			 - arbitrary_precision
			 - float_roundtrip
			 - indexmap
			 - preserve_order
			 - raw_value
			 - unbounded_depth

~/rust/zipapi$ explorer.exe .

~/rust/zipapi$ cargo build
  Downloaded futures-sink v0.3.31
  Downloaded openssl-macros v0.1.1
  Downloaded try-lock v0.2.5
  Downloaded want v0.3.1
  Downloaded tinyvec_macros v0.1.1
  Downloaded foreign-types v0.3.2
  Downloaded equivalent v1.0.1
  Downloaded http-body v1.0.1
  Downloaded foreign-types-shared v0.1.1
  Downloaded sync_wrapper v1.0.1
  Downloaded openssl-probe v0.1.5
  Downloaded tower-service v0.3.3
  Downloaded form_urlencoded v1.2.1
  Downloaded autocfg v1.4.0
  Downloaded percent-encoding v2.3.1
  Downloaded pkg-config v0.3.31
  Downloaded subtle v2.6.1
  Downloaded slab v0.4.9
  Downloaded futures-task v0.3.31
  Downloaded http-body-util v0.1.2
  Downloaded futures-core v0.3.31
  Downloaded atomic-waker v1.1.2
  Downloaded hyper-tls v0.6.0
  Downloaded zeroize v1.8.1
  Downloaded shlex v1.3.0
  Downloaded pin-utils v0.1.0
  Downloaded itoa v1.0.11
  Downloaded untrusted v0.9.0
  Downloaded tokio-native-tls v0.3.1
  Downloaded serde_urlencoded v0.7.1
  Downloaded native-tls v0.2.12
  Downloaded fnv v1.0.7
  Downloaded ipnet v2.10.1
  Downloaded futures-channel v0.3.31
  Downloaded once_cell v1.20.2
  Downloaded hyper-rustls v0.27.3
  Downloaded mime v0.3.17
  Downloaded rustls-pemfile v2.2.0
  Downloaded tokio-rustls v0.26.0
  Downloaded httparse v1.9.5
  Downloaded pin-project-lite v0.2.15
  Downloaded tinyvec v1.8.0
  Downloaded spin v0.9.8
  Downloaded socket2 v0.5.7
  Downloaded rustls-pki-types v1.10.0
  Downloaded tracing v0.1.40
  Downloaded ryu v1.0.18
  Downloaded bytes v1.8.0
  Downloaded tracing-core v0.1.32
  Downloaded openssl-sys v0.9.104
  Downloaded unicode-bidi v0.3.17
  Downloaded cc v1.1.31
  Downloaded url v2.5.2
  Downloaded hyper-util v0.1.10
  Downloaded serde v1.0.214
  Downloaded base64 v0.22.1
  Downloaded mio v1.0.2
  Downloaded indexmap v2.6.0
  Downloaded http v1.1.0
  Downloaded tokio-util v0.7.12
  Downloaded unicode-normalization v0.1.24
  Downloaded serde_json v1.0.132
  Downloaded hashbrown v0.15.0
  Downloaded hyper v1.5.0
  Downloaded h2 v0.4.6
  Downloaded futures-util v0.3.31
  Downloaded reqwest v0.12.9
  Downloaded rustls-webpki v0.102.8
  Downloaded vcpkg v0.2.15
  Downloaded openssl v0.10.68
  Downloaded idna v0.5.0
  Downloaded rustls v0.23.16
  Downloaded tokio v1.41.0
  Downloaded encoding_rs v0.8.35
  Downloaded ring v0.17.8
  Downloaded 75 crates (10.8 MB) in 3.10s (largest was `ring` at 4.2 MB)
   Compiling libc v0.2.161
   Compiling bytes v1.8.0
   Compiling pin-project-lite v0.2.15
   Compiling shlex v1.3.0
   Compiling cc v1.1.31
   Compiling vcpkg v0.2.15
   Compiling proc-macro2 v1.0.89
   Compiling once_cell v1.20.2
   Compiling itoa v1.0.11
   Compiling pkg-config v0.3.31
   Compiling futures-core v0.3.31
   Compiling unicode-ident v1.0.13
   Compiling autocfg v1.4.0
   Compiling openssl-sys v0.9.104
   Compiling mio v1.0.2
   Compiling socket2 v0.5.7
   Compiling fnv v1.0.7
   Compiling http v1.1.0
   Compiling tokio v1.41.0
   Compiling slab v0.4.9
   Compiling quote v1.0.37
   Compiling tracing-core v0.1.32
   Compiling syn v2.0.85
   Compiling hashbrown v0.15.0
   Compiling equivalent v1.0.1
   Compiling cfg-if v1.0.0
   Compiling futures-task v0.3.31
   Compiling foreign-types-shared v0.1.1
   Compiling pin-utils v0.1.0
   Compiling futures-sink v0.3.31
   Compiling openssl v0.10.68
   Compiling httparse v1.9.5
   Compiling indexmap v2.6.0
   Compiling futures-util v0.3.31
   Compiling foreign-types v0.3.2
   Compiling tracing v0.1.40
   Compiling http-body v1.0.1
   Compiling try-lock v0.2.5
   Compiling serde v1.0.214
   Compiling native-tls v0.2.12
   Compiling tinyvec_macros v0.1.1
   Compiling tokio-util v0.7.12
   Compiling atomic-waker v1.1.2
   Compiling bitflags v2.6.0
   Compiling tinyvec v1.8.0
   Compiling h2 v0.4.6
   Compiling want v0.3.1
   Compiling futures-channel v0.3.31
   Compiling smallvec v1.13.2
   Compiling openssl-probe v0.1.5
   Compiling log v0.4.22
   Compiling openssl-macros v0.1.1
   Compiling percent-encoding v2.3.1
   Compiling form_urlencoded v1.2.1
   Compiling unicode-normalization v0.1.24
   Compiling unicode-bidi v0.3.17
   Compiling hyper v1.5.0
   Compiling ryu v1.0.18
   Compiling tower-service v0.3.3
   Compiling hyper-util v0.1.10
   Compiling tokio-native-tls v0.3.1
   Compiling idna v0.5.0
   Compiling http-body-util v0.1.2
   Compiling rustls-pki-types v1.10.0
   Compiling serde_json v1.0.132
   Compiling rustls-pemfile v2.2.0
   Compiling url v2.5.2
   Compiling hyper-tls v0.6.0
   Compiling serde_urlencoded v0.7.1
   Compiling encoding_rs v0.8.35
   Compiling sync_wrapper v1.0.1
   Compiling mime v0.3.17
   Compiling base64 v0.22.1
   Compiling memchr v2.7.4
   Compiling ipnet v2.10.1
   Compiling reqwest v0.12.9
   Compiling zipapi v0.1.0 (/home/ore/rust/zipapi)
error[E0433]: failed to resolve: could not find `main` in `tokio`
  --> src/main.rs:21:10
   |
21 | #[tokio::main]
   |		  ^^^^ could not find `main` in `tokio`

error: cannot find derive macro `Deserialize` in this scope
 --> src/main.rs:6:17
  |
6 | #[derive(Debug, Deserialize)]
  |				 ^^^^^^^^^^^
  |
note: `Deserialize` is imported here, but it is only a trait, without a derive macro
 --> src/main.rs:2:5
  |
2 | use serde::Deserialize;
  |	 ^^^^^^^^^^^^^^^^^^

error: cannot find derive macro `Deserialize` in this scope
  --> src/main.rs:15:17
   |
15 | #[derive(Debug, Deserialize)]
   |				 ^^^^^^^^^^^
   |
note: `Deserialize` is imported here, but it is only a trait, without a derive macro
  --> src/main.rs:2:5
   |
2  | use serde::Deserialize;
   |	 ^^^^^^^^^^^^^^^^^^

error[E0599]: no method named `json` found for struct `Response` in the current scope
  --> src/main.rs:26:22
   |
26 |	 let body = response.json::<ZipCloudResponse>().await?;
   |						 ^^^^ method not found in `Response`

error[E0752]: `main` function is not allowed to be `async`
  --> src/main.rs:22:1
   |
22 | async fn main() -> Result<()> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

Some errors have detailed explanations: E0433, E0599, E0752.
For more information about an error, try `rustc --explain E0433`.
error: could not compile `zipapi` (bin "zipapi") due to 5 previous errors

~/rust/zipapi$cargo build
	Updating crates.io index
  Downloaded sync_wrapper v0.1.2
  Downloaded scopeguard v1.2.0
  Downloaded lock_api v0.4.12
  Downloaded hyper-tls v0.5.0
  Downloaded signal-hook-registry v1.4.2
  Downloaded tokio-macros v2.4.0
  Downloaded httpdate v1.0.3
  Downloaded http-body v0.4.6
  Downloaded parking_lot_core v0.9.10
  Downloaded rustls-pemfile v1.0.4
  Downloaded serde_derive v1.0.214
  Downloaded parking_lot v0.12.3
  Downloaded base64 v0.21.7
  Downloaded http v0.2.12
  Downloaded reqwest v0.11.27
  Downloaded hyper v0.14.31
  Downloaded h2 v0.3.26
  Downloaded 17 crates (978.6 KB) in 2.15s
   Compiling lock_api v0.4.12
   Compiling parking_lot_core v0.9.10
   Compiling smallvec v1.13.2
   Compiling scopeguard v1.2.0
   Compiling tokio-macros v2.4.0
   Compiling signal-hook-registry v1.4.2
   Compiling http v0.2.12
   Compiling parking_lot v0.12.3
   Compiling serde v1.0.214
   Compiling tokio v1.41.0
   Compiling serde_derive v1.0.214
   Compiling httpdate v1.0.3
   Compiling http-body v0.4.6
   Compiling base64 v0.21.7
   Compiling rustls-pemfile v1.0.4
   Compiling sync_wrapper v0.1.2
   Compiling tokio-util v0.7.12
   Compiling tokio-native-tls v0.3.1
   Compiling h2 v0.3.26
   Compiling serde_urlencoded v0.7.1
   Compiling serde_json v1.0.132
   Compiling hyper v0.14.31
   Compiling hyper-tls v0.5.0
   Compiling reqwest v0.11.27
   Compiling zipapi v0.1.0 (/home/ore/rust/zipapi)
warning: fields `address1`, `address2`, `address3`, `prefcode`, and `zipcode` are never read
  --> src/main.rs:8:2
   |
7  | struct Address {
   |		------- fields in this struct
8  |	 address1: String,
   |	 ^^^^^^^^
9  |	 address2: String,
   |	 ^^^^^^^^
10 |	 address3: String,
   |	 ^^^^^^^^
11 |	 prefcode: String,
   |	 ^^^^^^^^
12 |	 zipcode: String,
   |	 ^^^^^^^
   |
   = note: `Address` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   = note: `#[warn(dead_code)]` on by default

warning: fields `status` and `results` are never read
  --> src/main.rs:17:2
   |
16 | struct ZipCloudResponse {
   |		---------------- fields in this struct
17 |	 status: u32,
   |	 ^^^^^^
18 |	 results: Vec<Address>,
   |	 ^^^^^^^
   |
   = note: `ZipCloudResponse` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: `zipapi` (bin "zipapi") generated 2 warnings
	Finished dev [unoptimized + debuginfo] target(s) in 1m 38s

~/rust/zipapi$ cargo run
warning: fields `address1`, `address2`, `address3`, `prefcode`, and `zipcode` are never read
  --> src/main.rs:8:2
   |
7  | struct Address {
   |		------- fields in this struct
8  |	 address1: String,
   |	 ^^^^^^^^
9  |	 address2: String,
   |	 ^^^^^^^^
10 |	 address3: String,
   |	 ^^^^^^^^
11 |	 prefcode: String,
   |	 ^^^^^^^^
12 |	 zipcode: String,
   |	 ^^^^^^^
   |
   = note: `Address` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   = note: `#[warn(dead_code)]` on by default

warning: fields `status` and `results` are never read
  --> src/main.rs:17:2
   |
16 | struct ZipCloudResponse {
   |		---------------- fields in this struct
17 |	 status: u32,
   |	 ^^^^^^
18 |	 results: Vec<Address>,
   |	 ^^^^^^^
   |
   = note: `ZipCloudResponse` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis

warning: `zipapi` (bin "zipapi") generated 2 warnings
	Finished dev [unoptimized + debuginfo] target(s) in 0.60s
	 Running `target/debug/zipapi`
ZipCloudResponse { status: 200, results: [Address { address1: "東京都", address2: "千代田区", address3: "皇居外苑", prefcode: "13", zipcode: "1000002" }] }
~/rust/zipapi$




サンプルコード

use reqwest::Client;
use serde::Deserialize;

type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

#[derive(Debug, Deserialize)]
struct Address {
	address1: String,
	address2: String,
	address3: String,
	prefcode: String,
	zipcode: String,
}

#[derive(Debug, Deserialize)]
struct ZipCloudResponse {
	status: u32,
	results: Vec<Address>,
}

#[tokio::main]
async fn main() -> Result<()> {
	let client = Client::new();
	let url = "https://zipcloud.ibsnet.co.jp/api/search";
	let response = client.get(url).query(&[("zipcode", "1000002")]).send().await?;
	let body = response.json::<ZipCloudResponse>().await?;
	println!("{:?}", body);
	Ok(())
}


以上

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?