製作 crate 併發布到 Crates-io

準備

發佈 crate 時, 一旦發佈無法修改, 無法覆蓋, 因此要注意郵箱等一些個人信息

訪問 crates.io 的 帳號設定頁面 [1], 生成 Token

並在命令行 執行 cargo login your token

此命令將告訴 Cargo 你的 API 令牌, 並將其存儲在本地 ~/.cargo/credentials

crates.iocrate的名字, 會採取先到先得的方式分配.


打包 & 發佈

對於 Cargo.toml:

[package]
name = "dashen"
version = "0.1.1"
authors = ["xxxx <x@xxxxxx.tech>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ferris-says = "0.2"

執行cargo publish:

這是因爲缺少一些關鍵信息:關於該 crate 用途的描述和用戶可能在何種條款下使用該 crate 的 license

想要修正這個錯誤, 需要在 Cargo.toml 中引入這些信息.

描述通常是一兩句話, 它會出現在 crate 的搜索結果中和 crate 頁面裏.

對於 license 字段, 需要一個 license 標識符值(license identifier value)

Linux 基金會的 Software Package Data Exchange (SPDX)[2] 列出了可以使用的標識符

例如指定 crate 使用 MIT License, 可增加 MIT 標識符

[package]
name = "dashen"
version = "0.1.1"
authors = ["xxxx <xxx@xxxxx.tech>"]
edition = "2018"
description = "the first crate by xxxxx"
license = "MIT"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ferris-says = "0.2"

再次執行 cargo publish:

這是因爲沒有指定 git 倉庫

新建一個倉庫, 指定爲遠程倉庫, 並提交代碼

再次執行 cargo publish:

此時也能在 crates.io[3] 搜到剛剛發佈的 crate


英文版文檔:

Publishing a Crate to Crates.io[4]

中文版文檔:

將 crate 發佈到 Crates.io[5]

參考資料

[1]

帳號設定頁面: https://crates.io/me

[2]

Software Package Data Exchange (SPDX): http://spdx.org/licenses/

[3]

crates.io: https://crates.io/search?page=1&per_page=10&q=dashen

[4]

Publishing a Crate to Crates.io: https://doc.rust-lang.org/book/ch14-02-publishing-to-crates-io.html

[5]

將 crate 發佈到 Crates.io: https://kaisery.github.io/trpl-zh-cn/ch14-02-publishing-to-crates-io.html

本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源https://mp.weixin.qq.com/s/0TJdBcwyPQQIBhGGZoqo_w