Skip to content

Instantly share code, notes, and snippets.

@craftslab
Last active April 2, 2022 03:29
Show Gist options
  • Save craftslab/1a808b48d7f8db7b60e75d1d3c9b6038 to your computer and use it in GitHub Desktop.
Save craftslab/1a808b48d7f8db7b60e75d1d3c9b6038 to your computer and use it in GitHub Desktop.
hello rust

Install

# Update for stable
rustup update --no-self-update stable

# Install for coverage
cargo install cargo-tarpaulin

# Install for lint
cargo install cargo-hack --debug
rustup component add clippy
rustup component add rustfmt

# Install for upx
sudo apt update -y
sudo apt install -y upx

# Install for Windows
# rustc --print target-list
sudo apt update -y
sudo apt install -y mingw-w64
rustup target add x86_64-pc-windows-gnu
rustup toolchain install stable-x86_64-pc-windows-gnu

# Install for Linux
# rustc --print target-list
rustup target add x86_64-unknown-linux-musl
rustup toolchain install stable-x86_64-unknown-linux-musl

Create

cargo new hello-rust

Check

cd hello-rust

cargo check --release --all-features --all-targets

Build

cd hello-rust

# Build target for Windows
cargo build --release --all-features --all-targets --target x86_64-pc-windows-gnu
upx target/x86_64-pc-windows-gnu/release/hello-rust.exe

# Build target for Linux
cargo build --release --all-features --all-targets --target=x86_64-unknown-linux-musl
upx target/x86_64-unknown-linux-musl/release/hello-rust

Lint

cd hello-rust

# rustup update --no-self-update stable
# rustup component add rustfmt
cargo fmt --all

# rustup update --no-self-update stable
# rustup component add clippy
cargo clippy --all-features --all-targets -- -A clippy::all

# rustup update --no-self-update stable
# cargo install cargo-hack --debug
cargo hack check --all-features --all-targets

Test

cd hello-rust

# rustup update --no-self-update stable
cargo test --all-features --all-targets -- --nocapture

Clean

cd hello-rust

rm -rf target

Reference

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[package]
name = "hello-rust"
version = "0.0.1"
edition = "2021"
rust-version = "1.59"
authors = ["Jia Jia <angersax@sina.com>"]
description = "Hello Rust"
repository = "https://github.com/craftslab/hello-rust"
documentation = "https://github.com/craftslab/hello-rust"
homepage = "https://github.com/craftslab/hello-rust"
keywords = ["hello", "rust"]
categories = ["rust"]
license = "Apache-2.0"
readme = "README.md"
include = [
"src/**/*",
"Cargo.toml",
"README.md"
]
[[bin]]
name = "hello-rust"
path = "src/main.rs"
[profile.release]
strip = "debuginfo"
[dependencies]
ferris-says = "0.2"
[badges]
maintenance = { status = "actively-developed" }
# .cargo/config
[source.crates-io]
registry = "https://github.com/rust-lang/crates.io-index"
replace-with = 'tuna'
[source.tuna]
registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"
[source.ustc]
registry = "git://mirrors.ustc.edu.cn/crates.io-index"
[source.sjtu]
registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index"
[source.rustcc]
registry = "git://crates.rustcc.cn/crates.io-index"
//! src/main.rs
use ferris_says::say;
use std::io::{stdout, BufWriter};
fn main() {
let stdout = stdout();
let message = String::from("Hello fellow Rustaceans!");
let width = message.chars().count();
let mut writer = BufWriter::new(stdout.lock());
say(message.as_bytes(), width, &mut writer).unwrap();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment