# Create a minimal project with "cargo init --unsafe"

**URL:** <https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331>\
**Category:** Uncategorized\
**Created:** [August 14, 2023, 11:13pm UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331 "2023-08-14T23:13:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![fps](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fps/32/11182_2.png) [@fps](https://internals.rust-lang.org/u/fps)\
**Post date:** [August 14, 2023, 11:13pm UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331/1 "2023-08-14T23:13:28Z")

</div>

Is this possible ?

"Create a minimal project that allows us to use custom allocators without using the "unsafe" keyword in all parts of the project, similar to Zig and C, by executing the command "cargo init --unsafe"."

Objective:

- Reduce project size.
- Decrease binary size.
- Speed up compilation time.
- Precise control over memory and manual management.

and other benefits for those who need to manually manage memory in their projects.

---

<div class="post-metadata">

**Author:** ![scottmcm](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/scottmcm/32/2355_2.png) [@scottmcm](https://internals.rust-lang.org/u/scottmcm)\
**Post date:** [August 15, 2023, 12:11am UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331/2 "2023-08-15T00:11:25Z")

</div>

`free` is C is UB if you pass it a bad pointer, so we're never going to let you `free` stuff outside of `unsafe` contexts.

[https://doc.rust-lang.org/std/alloc/trait.Allocator.html#tymethod.allocate](https://doc.rust-lang.org/std/alloc/trait.Allocator.html#tymethod.allocate) is safe, though, so you can allocate in safe code.

(Albeit using that method to allocate in safe code is useless, since you can't use the result in any useful way. But that's what `Box<T, CustomAlloc>::new` will be for, [once](https://shift.click/blog/allocator-trait-talk/) custom allocators are fully figured out.)

---

<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:** [August 15, 2023, 3:27am UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331/3 "2023-08-15T03:27:28Z")

</div>

I believe @fps wants a "globally unsafe" mode, where all code is considered `unsafe`. I've wanted for a similar thing a few times, in projects where most code is in an `unsafe` context, and even that which isn't is typically still soundness-relevant because of ambient soundness requirements.

But even in such environments, being able to say that a specific interface is safe — to define safe abstractions even among the unsafe context — is a strong benefit, and in retrospect each of those projects would have turned out worse had they just been ambiently unsafe.

A desirable strength of Rust is that decently written non-obfuscated Rust is generally fairly locally evident as to what the code is doing and what its safety obligations are, if any, at least given the availability of type lookup. If code might be doing `unsafe` operations without any local indication of such (i.e. the `unsafe` keyword), then a good deal of this property is lost.

If you absolutely must have something like this, a source preprocessor that adds `unsafe` blocks everywhere would be your best bet. Nobody is likely to provide such a tool, and the use of said tool would be generally looked down on as violating a core goal of Rust.

---

<div class="post-metadata">

**Author:** ![fps](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/fps/32/11182_2.png) [@fps](https://internals.rust-lang.org/u/fps)\
**Post date:** [August 15, 2023, 9:55am UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331/4 "2023-08-15T09:55:23Z")

</div>

Yes, thank you. I have added some of the intended goals of this action to the main post.

---

<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:** [August 15, 2023, 1:23pm UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331/5 "2023-08-15T13:23:31Z")

</div>

> [@fps](#):
>
> Create a minimal project that allows us to use custom allocators without using the "unsafe" keyword in all parts of the project

You can define a custom `#[global_allocator]`. Implementing that custom global allocator will probably involve `unsafe`, but "all parts of the project" besides that can continue to use the safe allocation API, e.g. `Box`, `String`, and `Vec` will continue to present safe allocation APIs.

---

<div class="post-metadata">

**Author:** ![dlight](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/dlight/32/8462_2.png) [@dlight](https://internals.rust-lang.org/u/dlight)\
**Post date:** [August 16, 2023, 5:35am UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331/6 "2023-08-16T05:35:26Z")

</div>

> [@CAD97](#):
>
> I believe @fps wants a "globally unsafe" mode, where all code is considered `unsafe`.

I'm not sure if this was ever proposed, but it would be interesting to have something like `#![unsafe]` or maybe `#[implictly_unsafe]` to mark that everything in a file implictly unsafe.. except maybe by things marked with `#[safe]` (or maybe even make `safe` a keyword)

This would still enable fine-grained annotation of safety, but with inverted polarity.

Like,

```rust
#![implictly_unsafe]

fn a(x: *mut i32) {
    *x = 1;
}

safe fn b(x: i32) {
    println!("Hello {x}");
}

fn c(mut x: i32) {
    a(&mut x as *mut i32);

    safe {
        b(x); // safe to call
    }
}

```

Becomes

```rust
unsafe fn a(x: *mut i32) {
    unsafe {
        *x = 10;
    }
}

fn b(x: i32) {
    println!("Hello {x}");
}

unsafe fn c(mut x: i32) {
    unsafe {
        a(&mut x as *mut i32);
    }

    b(x); // safe to call
}

```

But,

> [@CAD97](#):
>
> A desirable strength of Rust is that decently written non-obfuscated Rust is generally fairly locally evident as to what the code is doing and what its safety obligations are, if any, at least given the availability of type lookup. If code might be doing `unsafe` operations without any local indication of such (i.e. the `unsafe` keyword), then a good deal of this property is lost.

This is true, so I'm unsure if this would be a net positive. (however this is sometimes obscured by some `#[attributes]` being silently unsafe, among other things; the list of unsafe things in Rust is somewhat non-uniform)

---

<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:** [November 14, 2023, 5:35am UTC](https://internals.rust-lang.org/t/create-a-minimal-project-with-cargo-init-unsafe/19331/7 "2023-11-14T05:35:50Z")

</div>

Closed on November 14, 2023, 5:35am UTC

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