官术网_书友最值得收藏!

Dependencies on libraries from crates.io

If a library that our program depends on is published on https://crates.io/, all we have to do to link it is add its linking code to the dependencies section. Let's say we want to use serde (a tool for turning Rust data into formats such as JSON and back) in our program. First, we find its linking code with: cargo search serde.

I originally found out about serde by browsing through crates.io, an exploration that I would encourage you to try as well.

This will print out a list of matches that looks something like this:


serde = "1.0.70" # A generic serialization/deserialization framework
serde_json = "1.0.24" # A JSON serialization file format
serde_derive_internals = "0.23.1" # AST representation used by Serde derive macros. Unstable.
serde_any = "0.5.0" # Dynamic serialization and deserialization with the format chosen at runtime
serde_yaml = "0.7.5" # YAML support for Serde
serde_bytes = "0.10.4" # Optimized handling of `&[u8]` and `Vec<u8>` for Serde
serde_traitobject = "0.1.0" # Serializable trait objects. This library enables the serialization of trait objects such…
cargo-ssearch = "0.1.2" # cargo-ssearch: cargo search on steroids
serde_codegen_internals = "0.14.2" # AST representation used by Serde codegen. Unstable.
serde_millis = "0.1.1" # A serde wrapper that stores integer millisecond value for timestamps and duration…
... and 458 crates more (use --limit N to see more)

The first one is the core serde library, and the linking code is the part of the line before the # symbol. All we have to do is copy and paste that into the dependencies section of Cargo.toml, and Rust will know that it should compile and link serde when it compiles our foo program. So, the dependencies section of Cargo.toml would look like this:

 [dependencies]
serde = "1.0.70"

主站蜘蛛池模板: 孝昌县| 三明市| 利津县| 方城县| 加查县| 涡阳县| 奉节县| 高碑店市| 会东县| 玛曲县| 玉田县| 顺昌县| 洛南县| 玉龙| 建湖县| 石家庄市| 泰来县| 烟台市| 永修县| 衡阳市| 卫辉市| 平和县| 司法| 巴青县| 岑溪市| 沁阳市| 桂东县| 湖州市| 玛纳斯县| 温泉县| 阳城县| 台北市| 错那县| 龙海市| 廊坊市| 阿瓦提县| 桂林市| 清水县| 德保县| 杭锦后旗| 茶陵县|