# Option to add replacement std dependency to subdependency's for no\_std platforms

**URL:** <https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661>\
**Category:** tools and infrastructure\
**Created:** [March 30, 2025, 2:46am UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661 "2025-03-30T02:46:35Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![garlicbready](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/garlicbready/32/13101_2.png) [@garlicbready](https://internals.rust-lang.org/u/garlicbready)\
**Post date:** [March 30, 2025, 2:46am UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/1 "2025-03-30T02:46:36Z")

</div>

I figured I'd try here first before raising a github feature request for cargo.

My understanding is typically with embedded platforms there's no standard library. Part of the reason for this is that std includes operating system functionality which isn't needed. But also std covers use of types and functionality assicoated with the heap such as String which isn't available by default.

This hasn't stopped people from trying though to gain access to some of the code libraries already out there. I've seen a few workarounds to this

- With the esp32 platform they've managed to setup they're own std library for ESP32 devices and variants via esp-idf.
- For the stm32 / rpi-pico devices you can use the embedded-alloc or nostd creates to get some of this functionality back (assuming you want to use a heap)
- [GitHub - nostd-rs/nostd: Simplify `no_std` development](https://github.com/nostd-rs/nostd/tree/main)
- [GitHub - rust-embedded/embedded-alloc: A heap allocator for embedded systems](https://github.com/rust-embedded/embedded-alloc)
- Someone tried a proof of concept using parts of the esp-idf  
[GitHub - tana/pico-std-rust: Proof-of-concept for using Rust std on RP2040](https://github.com/tana/pico-std-rust)

There's also been some use of `cargo +nightly -Zbuild-std=std build`  
but that's considered something of a hack and not supported / requires nightly.  
I think it's also been broken for a few months now as well due to thread\_local

- [document args for `-Zbuild-std` when folks aren't building everything · Issue #14889 · rust-lang/cargo · GitHub](https://github.com/rust-lang/cargo/issues/14889)
- [Using std in embedded Rust](https://blog.timhutt.co.uk/std-embedded-rust/index.html)

The end result seems to be lots of different kinds of hacks and fudges as a way of working around the problem if you want to use a library that references std in some small way. Some people you say you shouldn't be using a heap on an embedded device in the first place. Other people like myself like the idea of being able to use the device however we want.

For a top level crate that your working on, you can do something like this for example using the nostd library

```toml
[dependencies]
std = { package = "nostd", version = "0.1" }

```

We still have to add `#![no_std]` to the top of `main.rs` and we have to manually add in any use statements as this fake std won't be imported / use aliased automatically

```rust
use std::string::String;
use std::vec::Vec;

```

But it does seem the cleanest option so far assuming we use a heap (the rp2350 has 512Kb of memory which can be extended up to 16Mb with PSRAM)

Where this tends to fall down however is a dependency / library that's also trying to use String or some other heap related type. Currently there's no way to feed in a substitute std library into that dependency without forking it and making changes to it's Cargo.toml file.  
I looked into build.rs but that doesn't have the functionality to perform any form of substitution Programatic access to cargo as a library might be one option but that's not guaranteed to have a stable API I think.

As a suggestion, perhaps a simple option that allows adding a dependency to all packages underneath the current crate (subdepends) via something like a special dependencies.global option

```toml
[dependencies.global]
std = { package = "nostd", version = "0.1" }

```

Or even something that's more specific just for std replacement.

This wouldn't be the only thing you'd need to do though, typically with the normal std library, things tend to be auto imported (you can use String without `use std::string::String;` for example) So a code change sonewhere else (not sure if it would be under rustc) would be also needed to try and handle this somehow under `#![no_std]` environments.

The idea is to gain greater compatibility with existing libraries that were never written with no\_std in mind when they were originally written. Also to find a more consistent way to swap out std functionality across the board (for all subdepends) via something like a custom std crate that sits next to the main project.

---

<div class="post-metadata">

**Author:** ![bascule](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bascule/32/3057_2.png) [@bascule](https://internals.rust-lang.org/u/bascule)\
**Post date:** [March 30, 2025, 6:06am UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/2 "2025-03-30T06:06:17Z")

</div>

Have you taken a look at `std`-aware cargo?

> **[GitHub - rust-lang/wg-cargo-std-aware: Repo for working on "std aware cargo"](https://github.com/rust-lang/wg-cargo-std-aware)**
>
> Repo for working on "std aware cargo"

---

<div class="post-metadata">

**Author:** ![Ddystopia](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ddystopia/32/10915_2.png) [@Ddystopia](https://internals.rust-lang.org/u/Ddystopia)\
**Post date:** [March 30, 2025, 8:58am UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/3 "2025-03-30T08:58:27Z")

</div>

I'm `no_std` dev myself, but unfortunately I couldn't understand what is your proposal. Like, if some `no_std` (and no `alloc`) library is written like `no_std` library, what is going to change for you as a user? Don't you anyway need to change the library to actually make use of `std`?

And about alloc, there is a good reason to avoid it - on devices without MMU, your heap will soon become fragmented and your program will crash (OOM). This is not an issue for non-realtime 64 bit systems with MMU because fragmentation is limited to a single page, anything higher can be remapped freely.

---

<div class="post-metadata">

**Author:** ![garlicbready](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/garlicbready/32/13101_2.png) [@garlicbready](https://internals.rust-lang.org/u/garlicbready)\
**Post date:** [March 30, 2025, 12:03pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/4 "2025-03-30T12:03:46Z")

</div>

To try and explain, lets say I'm writing a project that uses `#![no_std]`  
I depend on a library written by someone else that uses `std::string::String;`  
This library is not aware of anything to do with no\_std or embedded platforms so fails to build because `std::string::String;` is not visible.  
In order to avoid making changes to this library (because there might be lots of them)

1. I specify to cargo in my own project that any dependencies I'm adding in should have they're dependencies (subdepends) modified to include a custom std library implementation, something like the following

```toml
[dependencies.global]
std = { package = "nostd", version = "0.1" }

```

Or Maybe

```toml
[dependencies.global]
std = { package = "my_custom_std_lib", version = "0.1", path = "../my_custom_std_lib" }

```

1. We have a feature or magic somewhere in the compiler level that allows us to automatically add use statements to the top of every source, including all the libraries we've added in as depends

```rs
use std::string::String;

```

In a sense emulating or replacing the std library (or just parts of it) with our own custom implementation. How the compiler magic would work I'm not sure maybe something like replacing `#![no_std]` with `#![custom_std]`

```rs
#![custom_std]
mod special_global {
    use std::string::String;
}  

```

Thanks for the advise on alloc btw

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [March 30, 2025, 12:10pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/5 "2025-03-30T12:10:34Z")

</div>

If a crate is using `std::string::String` then replacing `std` with `nostd` won't give you any benefit, since both crates' `String` are just re-export of `alloc::string::String`. The only non-reexported part of `nostd` is the `io` module.

---

<div class="post-metadata">

**Author:** ![garlicbready](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/garlicbready/32/13101_2.png) [@garlicbready](https://internals.rust-lang.org/u/garlicbready)\
**Post date:** [March 30, 2025, 1:24pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/6 "2025-03-30T13:24:13Z")

</div>

Sorry not sure I explained things very well. What I'm aiming for is a way to trick dependencies into using a different but similar implementation of std in a limited fashion (since the normal std doesn't exist) without the need to change any of they're code

So to give another example Lets say we have a top level crate called "myproject" and it has a cargo dependency called "mydepend"  
Both of these are for an embedded target setup with `#![no_std]`

## Use Magic shenanigans

Within the top level "myproject" we have some magic that looks like this

```rs
#![custom_std]
mod special_global {
    use nostd::string::String;
}

```

Now within "mydepend" the following code is transformed from this

```rust
fn example() {
    println!("Hello, world!");
    let _hello = String::from("Hello, world!");
}

```

To this

```rust
use nostd::string::String;

fn example() {
    println!("Hello, world!");
    let _hello = String::from("Hello, world!");
}

```

This is the first step

## Create Dependency shenanigans

Now that dependency has a line of code at the top to use nostd::string::String; in place of the original String definition which is missing because of `#![no_std]`

But it has no dependency on that library so it's going to fail to build.  
So next we need some magic within "myproject" Cargo.toml file to pass down some information to the "mydepend" crate that it needs some additional dependency

```toml
[package]
name = "myproject"
edition = "2024"
version = "0.1.0"

[dependencies.mydepend]
version = "1.0.0"
# Not sure if this is the best way to do it but you get the idea
add_depends = [
  nostd = "0.1"
]

# Or maybe something special like this for all dependencies
[dependencies.global]
add_depends = [
  nostd = "0.1"
]

```

Not sure if this makes sense but the idea is to allow for the use of existing libraries by effectively building up parts of your own std implementation (it doesn't need to be nostd, it could be something totally custom locally) for an embedded platform.

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [March 30, 2025, 1:54pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/7 "2025-03-30T13:54:20Z")

</div>

> [@garlicbready](#):
>
> What I'm aiming for is a way to trick dependencies into using a different but similar implementation of std in a limited fashion (since the normal std doesn't exist) without the need to change any of they're code

I've understood that, but this is just how you're trying to solve a problem, it is not (or, should not be) the goal itself.

What issue are you actually trying to solve?

---

<div class="post-metadata">

**Author:** ![mathstuf](https://avatars.discourse-cdn.com/v4/letter/m/958977/32.png) [@mathstuf](https://internals.rust-lang.org/u/mathstuf)\
**Post date:** [March 30, 2025, 2:19pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/8 "2025-03-30T14:19:13Z")

</div>

I don't think the Linux kernel is using `cargo`, but it replaces `std` AFAIK and has also looked to import external crates (not sure if any are actually merged or not). AFAIK, it does have different allocation API patterns, but it might still have useful tidbits for something like this.

---

<div class="post-metadata">

**Author:** ![garlicbready](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/garlicbready/32/13101_2.png) [@garlicbready](https://internals.rust-lang.org/u/garlicbready)\
**Post date:** [March 30, 2025, 2:24pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/9 "2025-03-30T14:24:15Z")

</div>

> [@SkiFire13](#):
>
> What issue are you actually trying to solve?

I'm trying to import a library that might not be no\_std aware as a dependency then add in whatever functionality it's dependent on from std to get it to work without having to make any changes to that library

Edit I stumbled across this which also discusses different ways of patching the sysroot

> <https://github.com/rust-lang/wg-cargo-std-aware/issues/51#issuecomment-708587571>
>
> This issue is to discuss the strategy for removing the \`rustc-dep-of-std\` featur…e and the \`rust-std-workspace-\*\` packages which allows standard library crates to use crates from crates.io. This is relatively low priority, but will be a nice-to-have once stdlib dependencies are supported (and possibly stabilized).
> 
> Alex sketched out an outline at https://github.com/rust-lang/wg-cargo-std-aware/issues/27#issuecomment-529021295.  
> 
> I have been working on explicit dependencies, so I've been thinking about this lately. Assuming Cargo has explicit stdlib dependencies, and they are stabilized, then these crates can be modified to use explicit stdlib dependencies instead of \`rustc-dep-of-std\`. When built under normal circumstances, Cargo will pass pathless \`--extern\` flags to cause \`rustc\` to add those dependencies to the extern prelude, and rely on the resolver to load those crates from the sysroot. A complication with how rustbuild works remains: how will Cargo know what to do with these dependencies when building the standard library when there is no sysroot?
> 
> There are a few approaches:
> 
> \#### Use \`build-std\`
> 
> Rustbuild would use the \`build-std\` mode of Cargo to build the standard library. It would somehow need to tell Cargo the location of the stdlib source (which is in the workspace root, not the sysroot). Cargo would also need to detect when it is building a std dependency to properly draw the dependency edge to the real thing. Rustbuild would also need a base package to build, since it needs something to kick it off. This could be an empty library.
> 
> I'm not a big fan of this approach. Overall it seems messy, and much less flexible. I'm also not sure how features could be passed in to the standard library.
> 
> \#### Normal build with \`explicit-std\`
> 
> Rustbuild would do a build of the standard library the same way it does today. The only difference is that it needs to tell Cargo that explicit stdlib dependencies shouldn't use the sysroot (since it is in the process of building it). We instead want Cargo to pass \`--extern core=deps/libcore.rlib\` style options with the correct path.
> 
> Some suggestions for how to tell Cargo to behave this way:
> \- Have Cargo automatically detect, when it sees an explicit std dependency, to look in the build graph for a crate of the same name and use that instead. For example, a crate has a stdlib dependency on \`core\`, and \`core\` is being built, draw a dependency edge to the \`core\` unit.
> - Possibly add a perma-unstable flag to enable this mode if it seems too magical or risky.
> - Similar logic would need to be employed when running in \`build-std\` mode. This does have an intriguing consequence that in \`build-std\` mode, users could replace stdlib crates just by adding a normal dependency. \`build-std\` could restrict the search to the std graph, and not the user graph, if that seems too wild.
> \- Have some sort of "patch" syntax, where the stdlib dependencies could be patched to a local path. This has some complications, since this patch is only needed when building the standard library, but nothing else.
> 
> I'm enticed by the automatic option, since it "just works" and is relatively simple. Alex wasn't too enthusiastic when I mentioned it though 😜 .

---

<div class="post-metadata">

**Author:** ![pitaj](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/pitaj/32/11262_2.png) [@pitaj](https://internals.rust-lang.org/u/pitaj)\
**Post date:** [March 30, 2025, 8:19pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/10 "2025-03-30T20:19:36Z")

</div>

So you want a way to use an existing library, where that library is not already `no_std` and uses `std` APIs.

Your proposed solution is a way to patch that library to resolve `std` APIs to some crate that isn't the `std` provided by Rust.

I think a much better option would be to just patch that library to make it `no_std` in the first place.

---

<div class="post-metadata">

**Author:** ![garlicbready](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/garlicbready/32/13101_2.png) [@garlicbready](https://internals.rust-lang.org/u/garlicbready)\
**Post date:** [March 31, 2025, 5:14pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/11 "2025-03-31T17:14:00Z")

</div>

I think the problem with that approach is it defeats the purpose of abstraction.  
When you use an type such as a String or Vec, ideally you know it works but the implementation is hidden. All you know is that it has a set of methods and an expected behaviour. It's part of the reason for an interface, in a sense your plugging in an alternative implementation.

Typically you may have lots and lots of libraries in the wider world. Patching each one individually for a nonstd use case seems counter productive long term. Although I admit there will be use cases where that is needed. I think this is part of the reason for all the different hacks out there

I did run across [GitHub - japaric/xargo: The sysroot manager that lets you build and customize `std`](https://github.com/japaric/xargo) recently but it's in maintenance mode probably due to the -Zbuild-std feature (which currently has issues in my case)

I'm going to do some more digging though anyway since there's a lot I admit I don't understand  
yet (such as heap fragmentation). Typically when you get to a certain level of complexity it's sometimes better to do something via an api rather than configuration settings.

I'm now wondering if a way to do this would be to tap into cargo as a library via a form of wrapper on top although from what I can gather I don't think cargo has a stable or standardised api  
but it might offer a way to introduce an example or suggested way of doing something before introduced proper via an rfc or whichever means is needed

---

<div class="post-metadata">

**Author:** ![kpreid](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/kpreid/32/8484_2.png) [@kpreid](https://internals.rust-lang.org/u/kpreid)\
**Post date:** [March 31, 2025, 5:18pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/12 "2025-03-31T17:18:38Z")

</div>

> [@garlicbready](#):
>
> Patching each one individually for a nonstd use case seems counter productive long term.

I think that we should be looking for ways to make it easier for libraries to be `no_std` compatible _by default_ — make it not "nonstandard" any more.

---

<div class="post-metadata">

**Author:** ![danjl1100](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/danjl1100/32/6466_2.png) [@danjl1100](https://internals.rust-lang.org/u/danjl1100)\
**Post date:** [March 31, 2025, 5:35pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/13 "2025-03-31T17:35:16Z")

</div>

> [@kpreid](#):
>
> make it easier for libraries to be `no_std` compatible _by default_ — make it not "nonstandard" any more.

A clippy lint may be a good place to start:

> <https://github.com/rust-lang/rust-clippy/issues/1569>
>
> If one crate in the dependency chain depends on libstd, and only uses re-exporte…d libcore items and/or items from other \`#!\[no\_std\]\` crates, the end result is that libstd gets pulled in even if every single other crate in the chain is \`#!\[no\_std\]\`.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [March 31, 2025, 10:33pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/14 "2025-03-31T22:33:19Z")

</div>

> [@SkiFire13](#):
>
> The only non-reexported part of `nostd` is the `io` module.

Since this is the case, it seems that replacing `std` mostly solves the case of _automatically_ patching crates written using `std::` to instead use `alloc::` and `core::`. It doesn't really assist in making any code no\_std compatible that isn't in spirit already compatible, it only makes the burden of making it compatible in body easier. In effect, it collapses the facade of std similar to the eventual goal of std as a crate making the std/alloc/core split into feature flags of std just like for any other crate.

---

<div class="post-metadata">

**Author:** ![Vorpal](https://avatars.discourse-cdn.com/v4/letter/v/aca169/32.png) [@Vorpal](https://internals.rust-lang.org/u/Vorpal)\
**Post date:** [April 1, 2025, 12:14pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/15 "2025-04-01T12:14:47Z")

</div>

> [@SkiFire13](#):
>
> The only non-reexported part of `nostd` is the `io` module.

This is not true. For example: `std::f32` has sin and cos, `core::f32` does not. I don't know if that is the only exception apart from io in std, but it is one I know of.

---

<div class="post-metadata">

**Author:** ![bjorn3](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bjorn3/32/2736_2.png) [@bjorn3](https://internals.rust-lang.org/u/bjorn3)\
**Post date:** [April 1, 2025, 1:22pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/16 "2025-04-01T13:22:38Z")

</div>

The nostd crate doesn't export sin or cos.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [September 23, 2026, 1:22pm UTC](https://internals.rust-lang.org/t/option-to-add-replacement-std-dependency-to-subdependencys-for-no-std-platforms/22661/17 "2026-09-23T13:22:45Z")

</div>

Closed on September 23, 2026, 1:22pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
