Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Add Cargo tips #42

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/content/configKinds/cargo-config-toml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
name: .cargo/config.toml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the name, Let's call this Cargo? Or Cargo Config? Would something like that work?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh i missed how there are two very similar kinds. But I notice they both have the same filepath in their kinds? Should these maybe be one type? I don't know much about rust/cargo

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue I found here is that project configuration is in a Cargo.toml file in the root of the project.

The actual configuration for the Cargo utility is in a .cargo/config.toml file and the two are exclusive, i.e. you can't mix configuration options in a Cargo.toml that belong in a .cargo/config.toml. So, I built two separate collections since it seemed like we were keying off the file name or configuration type.

Happy to rethink the way we're presenting Rust, though!

description: Cargo is the package manager and build tool for the Rust programming language. It's used to manage metadata, targets, dependencies, and more.
website: https://doc.rust-lang.org/stable/cargo/
logo: simple-icons:rust
body: |
The Cargo configuration file (`config.toml`) can be found in a variety of places:
- `my-project/.cargo/config.toml`
- `/.cargo/config.toml`
- `$CARGO_HOME/config.toml`

Using this file, you can configure all kinds of settings as they relate to building, aliasing commands, setting environment variables, configuring registry resolution, and more!
snippet:
lang: toml
filePath: "Cargo.toml"
code: |
[alias]
b = "build"
t = "test"

[build]
jobs = 16
rustc-wrapper = "/path/to/sccache"
target-dir = "target"

[cargo-new]
vcs = "hg"

[target.aarch64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld"]
27 changes: 27 additions & 0 deletions src/content/configKinds/cargo-toml.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: Cargo.toml
description: Cargo is the package manager and build tool for the Rust programming language. It's used to manage metadata, targets, dependencies, and more.
website: https://doc.rust-lang.org/stable/cargo/
logo: simple-icons:rust
body: >
`Cargo.toml` is a manifest file for Rust projects, which includes things like metadata (project name, author, license, etc.), a list of targets (i.e. binaries, libraries, etc.), and dependencies for the project.
In a standard Rust project, a Cargo.toml is in the root of the project and a sibling to the `src/` directory. Using Cargo, it generates a `Cargo.lock` file that can ensure predictable and reproducible dependency resolution between copies of the project.
snippet:
lang: toml
filePath: "Cargo.toml"
code: |
[package]
name = "my-rust-project"
version = "0.1.0"
edition = "2021"

[[bin]]
name = "my-binary"
path = "src/bin/main.rs"

[lib]
name = "libmy_binary"
path = "src/lib.rs"

[dependencies]
clap = { version = "4.4.0", features = ["derive"] }
22 changes: 22 additions & 0 deletions src/content/tips/cargo-linker.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
kind: cargo-config-toml
title: Changing Rust linker for faster compile times
description: |
Changing the linker for your Rust project can significantly speed up
compile times. Here's how to do it!
contributor: https://github.com/ELD
snippet: |
[target.aarch64-apple-darwin]
rustflags = ["-C", "link-arg=-fuse-ld=/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld"]

[target.target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=/usr/bin/mold"]
---

In the example snippet, we're setting the linker to use
[`mold`](https://github.com/rui314/mold) for Linux
targets and the new `ld-prime` linker on macOS (leveraging the Xcode 15 beta
command line tools).

Both linkers take advantage of multiple cores on your machine and significantly
speed up compile times.
18 changes: 18 additions & 0 deletions src/content/tips/cargo-patch-dep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
kind: cargo-toml
title: Patching a dependency in your project
description: |
Sometimes you want to patch a dependency in your project to incorporate a
patch or feature preview. This tip will show you how!
contributor: https://github.com/ELD
snippet: |
[dependencies]
cool_webframework = "0.1"

[patch.crates-io]
cool_webframework = { git = "https://github.com/foo/cool_webframework.git", branch = "preview_release" }
---

Sometimes, a crate you're using requires a fix or the feature or change you're
waiting for is in a pull request (and subsequently someone's fork of the repo).
To use that change, use the `[patch]` section of your `Cargo.toml`!
17 changes: 17 additions & 0 deletions src/content/tips/cargo-sccache.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
kind: cargo-config-toml
title: Add sccache to speed up your Rust build times
description: |
Use the `sccache` utility to cache compiled artifacts to speed up
successive Rust compile-times in addition to the speed of debug/incremental
builds.
contributor: https://github.com/ELD
snippet: |
[build]
rustc-wrapper = "/path/to/sccache"
---

Using the `sscache` compiler caching utility, you can speed up your successive
compilations by adding the following snippet to your Cargo configuration. This
wraps the `rustc` binary and allows it to search for precompiled artifacts
before resorting to compiling again.
1 change: 1 addition & 0 deletions src/pages/c/[configKind]/[tip].astro
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export async function getStaticPaths() {
});
return `<div class="rounded-md px-2 bg-gray-800 w-full overflow-x-auto"><pre><code class="language-${lang}">${html}</code></pre></div>`;
};

return marked(markdown, { renderer });
}

Expand Down
16 changes: 14 additions & 2 deletions src/pages/c/[configKind]/index.astro
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,23 @@ import type { BreadCrumbList } from '../../../components/Navigation/BreadCrumb.a

export async function getStaticPaths() {
const configKinds = await getCollection("configKinds");
const renderer = new marked.Renderer();

// Custom render lists in the body
renderer.list = (body, ordered) => {
const type = ordered ? 'ol' : 'ul';
const className = ordered ? 'list-decimal' : 'list-disc';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably text-left lists too.

image

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix this up in #48 since it deals with that change (this PR is rebased on top of that branch)


return `
<div class="text-left">
<${type} class="${className} list-inside text-left">${body}</${type}>
</div>`;
};

const paths = await Promise.all(configKinds.map(async (configKind) => {
const allTips = await getCollection("tips");
const tips = allTips.filter(tip => tip.data.kind.id === configKind.id);
const htmlBody = marked(configKind.data.body);
const htmlBody = marked(configKind.data.body, { renderer });
return {
params: { configKind: configKind.id },
props: { configKind, tips, htmlBody },
Expand Down Expand Up @@ -67,4 +79,4 @@ const breadCrumbList: BreadCrumbList = [
))}
</div>
</div>
</Layout>
</Layout>