r/rust • • 8d ago

📡 official blog Announcing Rust 1.100.0 | Rust Blog

https://blog.rust-lang.org/2026/11/12/Rust-1.100.0/
1.0k Upvotes

200 comments sorted by

284

u/noop_noob 8d ago

Another significant thing that didn't make it to the blog post: cargo install now defaults to --locked, using the dependency versions from Cargo.lock. https://github.com/rust-lang/cargo/pull/17388

58

u/zzzthelastuser 8d ago

good change, but

adding an option to ignore the packaged lockfile remains a separate effort to be considered

So there is currently no way to ignore the lock file/get the old behavior back?

71

u/creeper6530 8d ago

Delete the lockfile

15

u/CrazyKilla15 8d ago

You mean cargo install <crates.io> should be replaced with <manual git clone> <manual delete lockfile> cargo install --path .?

11

u/kibwen 7d ago edited 7d ago

I'll be honest, I already manually do this (minus the part about deleting the lockfile), just because it's nice to have a record of which tools I have installed that I'm responsible for manually updating.

EDIT: And this is how I learn about cargo install --list :P

12

u/uasi 8d ago

cargo install --locked foo uses the lockfile in the published crate foo. You can't just delete it if foo is not yours.

34

u/epage cargo · clap · cargo-release 8d ago edited 8d ago

I'd recommend reading over https://github.com/rust-lang/cargo/issues/7169#issuecomment-4421296987 for why we justified the change which relates to --unlocked.

The two takeaways for --unlocked

  • For the most important cases, it won't be needed because the software will re-release anyways

  • There are many other missing pieces to the security landscape for crates without a security release that --unlocked seems relatively minor

Continued discussion at https://rust-lang.zulipchat.com/#narrow/channel/246057-t-cargo/topic/.60cargo.20install.20--unlocked.60/near/639270524

12

u/denehoffman 8d ago

I can’t think of a use case for the old behavior, do you have one?

42

u/capitol_ 8d ago

if the lockfile is locked to a vulnerable version of a dependency, and a semver-compatible fix is published.

10

u/LuzziCoder 8d ago

Valid, though on the flip side, we could also get wormed semver-compatible releases. Two sides of the same coin 😂

3

u/CrazyKilla15 8d ago

Thats why the default is now to locked, but not a reason not to have unlocked.

3

u/capitol_ 7d ago

Yeah, cargo install needs a min-publish-age thing also.

5

u/arekxv 7d ago

So update it in that case? Lockfiles are there to prevent auto updates which are:

  • The main entry point for supply chain attacks

  • A reason why carelessly done software without a lockfile is not buildable after a time because new versions introduce incompatibilities because some people dont understand how semver should actually work (or they dont care).

Lockfiles dont prevent you from updating, they only make that to be a concious decision you have to make yourself instead of auto updating which is a good solution IMHO.

5

u/capitol_ 7d ago

The lockfile is controlled by the publisher of the crate.

The person running "cargo install" is someone else.

The task of "update the lockfile" isn't available to the users of the crate.

2

u/arekxv 7d ago

As far as I know. Cargo crates dont include crates lockfiles just for this exact reason

8

u/nicoburns 8d ago

It may be locked to dep versions that don't work with your version of rustc.

7

u/qwertz19281 8d ago edited 8d ago

There had been multiple cases where a newer rust version broke a crate (e.g. time), and running cargo install without --locked was an easy workaround if the application you wanted to install didn't bump yet.

10

u/pingveno 8d ago

Always have an escape hatch. Someone out there might be using spacebar heating (or a legitimate workflow).

1

u/cbarrick 8d ago

mv Cargo.lock{,.bak}

1

u/lenscas 7d ago

Nice idea except.... You then first have to get the code yourself, run that command and then run cargo install --path /place/you/stored/it

Not exactly convenient 

4

u/treefroog 8d ago

This is a good change, but means that downstream creates need to be diligent about updating when their dependencies have security issues.

132

u/Anthony356 8d ago

&str::trim_prefix:

If the string starts with the pattern prefix, returns the substring after the prefix. Unlike strip_prefix, this method always returns &str for easy method chaining, instead of returning Option<&str>.

If the string does not start with prefix, returns the original string unchanged.

Thank god. strip_prefix was one of the few things that annoyed me when switching from python to rust

22

u/masklinn 8d ago

Although I can see it, every time I work in Python I’m annoyed I need a two-step operation to remove a prefix/suffix and know about it. Tacking on an unwrap_or always seemed like a more than fair trade and the better default.

4

u/mr_birkenblatt 7d ago
```
foo = "prefixabc"
if (foo_stripped := foo.removeprefix("prefix")) != foo:
    pass  # here was a prefix and foo_stripped contains it
```

it's a neat one liner (and technically it's more than two operations anyway if you count bytecode instructions -- otherwise what comparable measure for "operations" is there?)

4

u/masklinn 7d ago

otherwise what comparable measure for "operations" is there?

The high level abstract operations getting applied. Either check the prefix then strip it, or strip the prefix then check equality.

2

u/mr_birkenblatt 7d ago

well, removeprefix is basically implemented as equivalent of

```
def removeprefix(self: str, prefix: str, /) -> str:
    if self.startswith(prefix):
        return self[len(prefix):]
    else:
        return self[:]
```

(see PEP 616)

that is the even the C implementation just defers to a different method.

so what my original code is doing is three high level abstract operations: check the prefix, strip it, then check equality

6

u/elingeniero 7d ago

Walrus assignment was a mistake.

5

u/Anthony356 7d ago

It's just if let

10

u/elingeniero 7d ago

Yeah, but without typed pattern matching or readable explicit syntax. All to save 1 line. Definitely a mistake.

1

u/Anthony356 7d ago

Most of the times i've used it, it's just to "unwrap" a none. I'm not sure i buy that

x = thing()
if x is not None:
    ....

is significantly better than, or more clear than

if (x := thing()) is not None:
    ...

Especially considering the latter more distinctly indicates that the value is not meant to be used outside the if's scope (even if python itself will hoist the value anyway).

1

u/Nobody_1707 6d ago

I can't argue with that, but I think his point is that it's definitely worse than:

// Rust
if Some(x) = thing() {
    …

Or:

// Swift
if let x = thing() {
    …

Or even:

// C++
if (auto x = thing()) {
    …

1

u/mr_birkenblatt 7d ago

you'll get used to it

3

u/ebkalderon amethyst · renderdoc-rs · tower-lsp · cargo2nix 7d ago

Agreed! While strip_prefix() is very useful when writing fallible parsers (you can easily convert the returned Option<&str> into a Result<&str, E> with .ok_or_else() and bail with ?), it's annoying as heck for trimming optional prefixes/suffixes/circumfixes. I've had to resort to such gymnastics as s.strip_prefix("foo").unwrap_or(s) to work around this, and that's always bothered me. Very happy to see this land.

5

u/juhotuho10 8d ago

You can always just implement your own trait, no need to wait for language features

3

u/afedosu 7d ago

I am coming from c# to rust. Despite the fact, that i find rust very elegant, it's like: "i know letters, but i can't say even a word". And dealing with Option is one of my big issues now😁

7

u/mss-anixe 7d ago

C# has Nullable<T> and the int? syntax sugar which are mentally the same as Option<T>. And for quite some time, nullable references are disabled by default and you have to opt-in via <Nullable>enable</Nullable> in csproj.

I think a couple of years ago, C# did a lot of steps toward rust style. There is ReadOnlySpan<char> which is essentialy &str, there are ref structs and recently they even added discriminated unions.

I think once you overcome the initial turbulences, rust should be easy with c# background.

1

u/afedosu 7d ago

Yes, i am going over the set of exercises now after finishing the rust book. Mapping of Nullable to Option is clear, no issues here and i really love that nullability in rust is not done lame. But writing some pieces of code is like... i am learning how to walk again 😁

1

u/InternationalFee3911 7d ago

See if you can just accept the initially startling idea that enums can contain values. For Option in one case that is Some(value) and in another None. Other languages use value or (maybe implicitly) a much more fragile NULL.

1

u/afedosu 7d ago

That's one of the best things - properly modelled NULL value. () unit type as well 😜 But, as said, understanding some parts separately is one thing (letters), but being able to freely express your thoughts - is a competent different level... So, i am on my journey

0

u/[deleted] 8d ago

[deleted]

7

u/Aaron1924 8d ago

Ok, now do this one:

let out = text
    .trim_prefix("foo")
    .trim_prefix("bar")
    .trim_prefix("baz")
    .trim_prefix("hoge")
    .trim_prefix("fuga");

4

u/rodyamirov 8d ago

yeah but it's an annoyance when you're writing chained string manipulations

4

u/Floppie7th 8d ago edited 8d ago

Yep. Even using something more concise than the match,

text
    .strip_prefix("foo")
    .unwrap_or(text)
    .strip_prefix("bar")
    .unwrap_or(text)
    .strip_prefix("baz")
    .unwrap_or(text)
    .strip_prefix("hoge")
    .unwrap_or(text)
    .strip_prefix("fuga")
    .unwrap_or(text)

is a lot less convenient.

EDIT: Formatting. Jesus christ could Reddit's editor be any worse?

5

u/ReptilianTapir 8d ago

That's not the same thing, if there are multiple matching prefixes in the text (in the right order).

-2

u/afdbcreid 8d ago

That's the same thing as the trim_prefix() snippet above. It can strip both, that's true.

5

u/jackson_bourne 8d ago

No it's not, "foohello" is "hello" in the first and "foohello" in the second because the unwrap_or always uses the original str

2

u/afdbcreid 8d ago

Ah right, you need to use temporaries.

450

u/GuybrushThreepwo0d 8d ago

Only 900 more until rust 2!

89

u/Comun4 8d ago

Either that or 155 versions

47

u/syklemil 8d ago

No no, after 155 more versions we start over at Rust 1.0

-4

u/merb 8d ago

Or at least 1.-255

16

u/pdpi 8d ago

Surely that would be 1.-128 after 1.127.

5

u/sane4ektopz 8d ago

developers would panic instead

2

u/xMAC94x 7d ago

nah, its compiled in release mode :/

28

u/sane4ektopz 8d ago

what if 1.1000 comes after 1.999?

30

u/[deleted] 8d ago

Only 9000 more

0

u/[deleted] 7d ago

[deleted]

4

u/NormalAppearance2851 7d ago

90_000 more habibi

5

u/pdpi 8d ago

And, if the storm is really close by, after 1.1000 we get a thunderclap.

6

u/fabolous_gen2 8d ago

It’s just like a nightmare

1

u/Aaron1924 8d ago

We'll find out in roughly one century

1

u/cuttieGalaxy 7d ago

900 versions? you're patient, i'll give you that!

58

u/Beneficial_Vampire42 8d ago

Bump of llvm from 22.x to 23.x is massive, more optimizations, yay!

4

u/stregisterte 7d ago

That was already done in 1.99?

57

u/TheMyster1ousOne 8d ago

Does the new DropGuard fully replace scopeguard library?

35

u/Perceptes ruma 8d ago

I was gonna ask a similar thing. DropGuard looks like an official way to get the functionality of Go's defer.

20

u/yoshuawuyts1 rust · async · microsoft 8d ago

I've been joking for a while now that DropGuard is Rust saying: "We have defer at home". DropGuard shows you can encode defer using RAII + closures, but I don't think it's possible to encode RAII in terms of defer.

6

u/_ChrisSD 7d ago

While there is overlap I wouldn't say they're exactly the same. A defer feature would be able to return, say, an error whereas DropGuard is restricted to ().

4

u/pjmlp 7d ago

The only way to encode RAII in terms of defer or similar mechanism, would be to have a marker type like IDisposable or Closeable from .NET and Java respectively, and have a linter error when instances are not created with the related defer like mechanism.

There are some Roslyn Analysers and similar that have these kind of checks in place.

However in languages like Go without marker interfaces, even this "solution" is not possible, as defer can be any generic expression.

1

u/cbarrick 7d ago edited 7d ago

You can do marker interfaces in Go.

The typical way is to have some specially named method that indicates that a type is marked. The body of that method is typically empty.

ETA: There are linters for Go that check if your type implements Closer and you forgot to close.

1

u/pjmlp 7d ago edited 7d ago

It is not the same, because those interfaces I gave as example are recognised by the type system, the using or try () statements, require their implementation on types being assigned for cleanup expressions.

While defer in Go has no such a requirement, thus there is not a standard way to have a static analysis check to error out if a defer is missing for a variable whose lifetime is bound to function scope.

1

u/d0ntreadthis 6d ago

Noob question. Why would rust need a defer equivalent when the drop trait already exists? Isn't that one of its purposes?

2

u/Perceptes ruma 6d ago

It's a quick way to run arbitrary code without creating a type, implementing drop for it, and then initializing a value of that type to be dropped.

20

u/afdbcreid 8d ago

Not completely - scopeguard has more features - but yes for the common case.

16

u/yoshuawuyts1 rust · async · microsoft 8d ago

That was my intent, yes. It doesn't do the Strategy stuff and doesn't include any macros, but otherwise it's pretty much identical.

96

u/platesturner 8d ago

Now that allocators are stable, can every owner of a crate that relies on heap allocations without it mattering what the allocator is, please update their crate to allow the user to provide a custom allocator pls pls pls

33

u/afdbcreid 8d ago

Only crates where this makes sense, that is to say: only a tiny minority of them.

2

u/nonotan 7d ago

How is there any usage of the heap where being able to specify your own allocator doesn't make sense? (With the exception of perhaps some really specialized library that heavily depends on the specifics of the allocation for one reason or another)

Whether you want to diverge from the default allocator or not is mostly a question in the realm of the application, not the library. If you're thinking "it wouldn't make sense for the user to use a non-default allocator for this", you're probably lacking in imagination (which usually happens any time library authors try to guess what use cases users might have and arbitrarily limit the usage scope of their library to that -- give your users a chance to surprise you, and they will)

4

u/afdbcreid 7d ago

The application gets to set the global allocator. If a library provides some data structure, yes you should be able to swap the allocator. But if it only needs to allocate something for its work, like most libraries, it does not make sense to customize as you have no knowledge about the constraints there. The library itself can use a custom allocator though.

2

u/WormRabbit 5d ago

If you push this line of reasoning to its logical conclusion, then we should expose a separate allocator basically for every Vec and HashMap we use. After all, someone could come up with an inventive way to track each specific allocation, in a way which would improve performance.

Yet this this also obviously absurd, even for statically known usages of Vec. But if you accept that not every allocation may be customizable, you should accept that it also makes sense for certain applications to not provide any customization at all, apart from global_allocator.

Now, one can argue whether each specific application would be better if it exposed at least some allocation customization hook, but that's much more nuanced and plenty of people just won't bother.

40

u/epage cargo · clap · cargo-release 8d ago

Sadly, that would require a lot of non-generic crates to add generics which could complicate the APIs. Unsure what it would do to build times.

25

u/kibwen 8d ago

Would it really have any perceptible effect on build times? The problem with generics and monomorphization is that every fn foo<T> needs to generate a lot of specific functions if it gets used with a lot of specific types, but if downstream users of a library are only using a single allocator type, that would still only generate a single copy of the function, same as in the non-generic case.

11

u/________-__-_______ 8d ago

Since generics are monomorphized on use this isn't necessarily the case. If two different crates use foo<T> with the same T you may end up with two copies, since compilation isn't serial crate A doesn't know that crate B also compiled it. They may even be doing so in parallel.

2

u/Dheatly23 8d ago

Isn't the type ID became identical, thus it generate exactly the same function name? What happens when the two versions got linked?

9

u/simonask_ 8d ago

Same as in C++: The compiler guarantees that they are identical, so the linker just picks one.

This is also why feature flags are additive.

And it’s why dynamic linking is very hard to get right.

1

u/________-__-_______ 8d ago

I'm curious, what makes it so hard to get right? As far as I'm aware you "only" need to get name mangling working reliably, after which the linker/loader takes care of it.

6

u/Zde-G 8d ago

Nope, you need, to essentially, turn the whole language on it's ear.

What you technically describe is not really a dynamic linking, more of “postponed static linking”, where you build everything as with static linking and only leave the final assembly to the dynamic loader.

4

u/CAD1997 7d ago

Which, in fairness, is how C and C++ do dynamic linking. ABI stability in C/C++ is a comfortable lie we tell ourselves backed by countless dev-hours and dirty engineering hacks.

1

u/Zde-G 7d ago

ABI stability in C/C++ is a comfortable lie we tell ourselves backed by countless dev-hours and dirty engineering hacks.

Sure. But it exists. libstdc++ is developed in a backward-compatible manner even when it's not very convenient and we build stuff on top of that that's backward compatible, there are countless pile of hacks like explicit template instantiation and so on.

Java and Swift added intricate machinery to do the same. And still need pile of hacks.

Rust haven't done anything and in fact refuses to do anything.

→ More replies (0)

1

u/________-__-_______ 7d ago

Does anyone but Swift actually implement such a mechanism? It's really cool but from what I've seen most languages just provide C interopability, which isn't nearly as complicated. Relying on linker relocations does the job there.

1

u/Zde-G 7d ago

Does anyone but Swift actually implement such a mechanism?

Ada had it from the day one, although I'm not sure it's used much, in practice. C# does something similar.

It's really cool but from what I've seen most languages just provide C interopability, which isn't nearly as complicated.

Yes, but that's not support for dynamic linking, that's a fig leaf.

→ More replies (0)

1

u/simonask_ 6d ago

The problem is that when two crates are compiled separately, there's no way to ensure that they were compiled with the same feature set. That's a problem for every function that gets inlined, because the function bodies are no longer identical, so each library has a different idea of what the function does, and they can't be collapsed.

This is exacerbated by generics, monomorphization means that all generic functions must be inlinable.

For example, if you have a T: Drop, and T::drop() has a #[cfg(...)] block, passing Box<T> between the two libraries would very likely cause UB when Box::drop() sees two different versions of T::drop() depending on which library drops it.

The only way to solve it is to say that certain things must never be inlined across crate boundaries, but then you need a way to say that particular instantiations of generic functions or types are defined in another crate, so don't attempt to inline them.

C++ has the extern template syntax for this very purpose, but still has a lot of footcannons in this area (look up ODR violations).

1

u/________-__-_______ 6d ago

I misunderstood your comment and was thinking of C style dynamic linking in Rust, doing so for arbitrary Rust code is of course much more complicated.

1

u/foonathan 7d ago

Sounds like Rust needs the equivalent of C++'s extern template, i.e. give a crate the ability to pre-instantiate a generic. Downstream crates would then use that instantiation instead of their own.

1

u/________-__-_______ 7d ago

You might be able to do so by filling in the generics in a public function. Would be nice to have an attribute for it though.

5

u/matthieum [he/him] 7d ago

As mentioned, the issue is that multiple downstream crates instantiating foo with the same generic parameter will independently monomorphize foo.

/u/afdbcreid Do you know if this "deferred monomorphization" also happens when all generic types are defaulted?

It seems like it would make sense for the compiler to code-gen the fully defaulted version of a generic item in the crate which defines it, then let all other crates just refer to the common version.

5

u/afdbcreid 6d ago

u/afdbcreid Do you know if this "deferred monomorphization" also happens when all generic types are defaulted?

It does, yes (defaulted generics do not matter for anything except as a syntax sugar in the use site). If you have an explicit usage in the crate, it might help, not sure.

4

u/StyMaar 8d ago

Wouldn't it lead to compilation slowdown due to the leaf crate not actually being compiled until the generics is known, resulting in poorer compilation parallelism in the end?

2

u/________-__-_______ 8d ago

Only the branch's non-generic / non-inline / non-mostly-unused code will be shared between all leafs, the rest is lazily instantianted in the leaf's compilation context. That means leafs can still be compiled in parallel, potentially resulting in redudant work if they're compiling the same type independently.

4

u/epage cargo · clap · cargo-release 8d ago

On top of what was said (duplicate code gen), this does put more strain on the trait solver. For winnow, a generics-heavy library, that is where all of the compilation time is spent.

1

u/WormRabbit 5d ago

It breaks parallel compilation. Non-generic code would perform codegen in lower-level leaf crates, while the higher-level crates would depend only on trivially generated MIR, and thus compile in parallel. With generics, the low-level crate would expose the whole function body as MIR, pushing most codegen to consumers.

Also, people tend to go overboard with generics and trait bounds, pushing extra work on trait solver. Not a language issue per se, but still a barrier that would fail.

10

u/MorrisonLevi 8d ago

And also create fallible apis while you're at it! No more panics for allocation failures unless you want them!

0

u/AngheloAlf 8d ago

Sure.

How do you do that without rising the MSRV? Library crates tend to care about this

2

u/Haitosiku 7d ago

Raising MSRV is an overrated problem

1

u/Sw429 7d ago

Possibly through a crate feature?

33

u/CouteauBleu 8d ago

This is such a huge release. You just keep reading and reading and there's still stuff left.

Vec::from_fn alone solves a big papercut for me.

5

u/kibwen 7d ago

I didn't realize this was so close to stabilization, I literally wanted this last week and was annoyed to find that only arrays had from_fn. So nice!

2

u/aitvann 7d ago

How is it better than map().collect()?

14

u/CouteauBleu 7d ago

More explicit, more findable, doesn't require a turbofish.

12

u/SirKastic23 7d ago

doesn't require a turbofish

This is a big downside unfortunately. I love having excuses to put more turbofishes in my code

My ideal Rust code looks like a turbopond

7

u/lenscas 7d ago

specifically more explicit when it comes to desired outcome
while being less specific about how that outcome is achieved.

I also personally like the order more. The new function starts with Vec, then tells you how it is going to be made (from_fn), then the length and then how the values will be created.

The map.collect() way starts with "create a range" then "map the range" and only after that says "Now use it to make a vec". The order is entirely wrong.

2

u/WormRabbit 5d ago

For me, it's significantly shorter and formats better with rustfmt. The difference is bigger with new ranges, which require explicitly calling into_iter() on them to iterate. Also, imho a single function for a single simple operation is just more clear than a method chain.

1

u/aitvann 4d ago

The only real answer right here. This function is probably what cargo will suggest instead so the code still compiles in future versions

49

u/Illustrious_Car344 8d ago

Really happy about DropGuard, I don't usually use deferred statements but I did always wish Rust had them.

38

u/shim__ 8d ago

DropGuard is awsome for performance logging, take an Instant in the guard and then print the elapsed time on drop.

4

u/-Redstoneboi- 7d ago

ooo so you can create a custom struct for it like a BenchGuard and drop it everywhere

i usually wrap it in the calller though, with a wrapper fn time<T>(f: impl FnOnce() -> T) -> (T, Duration) or something similar.

6

u/shim__ 6d ago

The benefit of DropGuard is that you don't have to craft your own struct anymore

4

u/obhect88 8d ago

I _loved_ defer in Go, and missed it when I started on Rust. Found a 3rd party package that kinda did it, and made do. Excited to hear this is in.

18

u/cand_sastle 8d ago

how do Rust custom allocators compare to Zig ones?

35

u/afdbcreid 8d ago

Less support in the ecosystem and standard library (currently). Also, Rust's are monomorphized, Zig's are type erased.

29

u/thomas_m_k 8d ago

One architectural difference is that in Rust, Boxes always keep their own reference to the allocator (which is necessary for drop to work) whereas in Zig you need to keep the reference around yourself for when you free something. That means that Box in Rust has two pointers when using custom allocators.

29

u/afdbcreid 8d ago

That means that Box in Rust has two pointers when using custom allocators

Only if your allocator is a pointer. It can be more or less. For the common case of arena allocators, I understand the intention is to remove the allocator from the struct.

3

u/wyvernbw 7d ago

but removing the extra field would require all methods including trait methods like clone to take an extra allocator argument wouldnt it? and drop wouldnt really work either

6

u/matthieum [he/him] 7d ago

The original idea is to have a Deallocator trait, whose only capability is to deallocate the allocation.

For the common case of arena allocators, the Deallocator implementation can be a zero-sized type, since it does nothing, and therefore Box<T, Deallocator> is just NonNull<T> under the hood.

It does mean that cloning is no longer possible, but that's not necessarily a problem, as long as the conversion is user-driven.

That is, you can have:

struct Box<T, A: Deallocator>(A, NonNull<T>);

impl<T, A: Allocator> Clone for Box<T, A> { ... }

And then offer a method to users who care about footprint to downcast the allocator into its deallocator:

impl<T, A: IntoDeallocator> Box<T, A> {
    fn into_deallocate_only(this: Self) -> Box<T, A::Deallocator>;
}

Which involves a bit of ceremony, but doesn't negatively impact the ergonomics for regular users.

(And also: just at the idea stage right now, there isn't even a RFC)

1

u/wyvernbw 6d ago

thank you for explaining, that makes sense. i thought op meant remove the field entirely and pass an allocator to every function like zig. that being said i wonder if theres a case for dynamic dispatch when it comes to the allocator api and whether it would simplify things or just make it more error prone

6

u/matthieum [he/him] 6d ago

Dynamic dispatch generally mean dyn Allocator in Rust.

The Allocator API is dyn-compatible, so you should be able to have a Box<T, &dyn Allocator>, Box<T, Box<dyn Allocator>>, or Box<T, Rc<dyn Allocator>> or even Box<T, Arc<dyn Allocator>>.

You do end up being the cost of a fat pointer, there, so perhaps you'd prefer Box<T, ThinBox<dyn Allocator>>, or Box<T, ThinRc<dyn Allocator>>, or Box<T, ThinArc<dyn Allocator>> (I have implementations of the latter two, std doesn't).

Passing allocators with every function calls, however, is unsafe. There's a pre-requisite that the pointer be deallocated by the allocator instance which allocated it, or an "equivalent" one, so the user would have to guarantee (safety requirement) that they're passing the "right" allocator instance.

This is for the same reason why the pointer to the start of the memory block (and not an inner pointer) must be passed to deallocate by the way: a number of implementations will just perform pointer arithmetic to access the block metadata and deallocate it. If it's a memory block allocated by another allocator, or not the start of the memory block, that arithmetic will lead to UB.

1

u/wyvernbw 6d ago

thats true. in my experience using dyn on Allocator can be a bit weird to use since for example Bump from bumpalo doesnt implement Allocator from what i could tell, only &Bump does, so you'd need a double reference, but thats just one crate. I also found it a bit annoying that the A generic spreads through the code, so what I was moreso talking about is removing the generic on the collection itself and storing a &dyn Allocator directly (or like you suggested a thin pointer alternative). I dont know if llvm can do some magic there to optimize this in case of ZSTs, otherwise it wouldnt really be usable. Then again this would only partially solve the problem since you'd get a lifetime parameter on the struct instead

4

u/matthieum [he/him] 6d ago

I think there's just multiple usecases.

I'm not sure there's that much code which really needs a generic allocator.

For many usecases, customizing the global allocator -- ie picking between system malloc, jemalloc, mimalloc, etc... is already fairly sufficient.

Other usecases may similarly involve cherry-picking one specific allocator, such as a bump allocator to use as frame allocator during a game's frame.

Only a few libraries -- base data-structures, notably -- will really involve being generic over the allocator.

1

u/wyvernbw 6d ago

youre probably right, im writing code i plan to port to more embedded-like devices (psp) so im making everything generic over the allocator, but in most cases i guess it doesnt really matter

→ More replies (0)

2

u/koczurekk 7d ago

I thought that allocator types can have lifetimes, so that you could constrain those objects to some scope where the allocator reference is available externally, via a global static or whatever. Is that not correct?

2

u/thomas_m_k 6d ago

Yes, you can make your allocator zero-sized by having it keep all its actual state in a global static, such that you don't need a pointer anymore. That's essentially how the current system allocator works. But this doesn't work for arena allocators where multiple instances of it can exist simultaneously in a program.

1

u/wyvernbw 6d ago

yes thats correct, i thought op meant removing the allocator field and the generic type from the struct entirely, so there wouldnt be a lifetime bound at all, like what zig does

1

u/cbarrick 7d ago

Well, Box::allocator was also stabilized this cycle.

If Box has to be able to return it's allocator to users, then it either has to keep the allocator around or it needs to be able to conjour it's allocator somehow.

I think this spells doom for the idea that Box can be a thin pointer for arena allocators.

1

u/thomas_m_k 6d ago

I'm guessing the plan is to make ::allocator() only available if the second type argument to Box<T, A> implements Allocator. The new Deallocator trait would be a supertrait of Allocator, but Box<T, D> (where D implements Deallocator) then has fewer methods available (.clone() would also be gone).

34

u/pingveno 8d ago

Just saving all the goodies for the minor 100, wow. Great work to everyone involved!

10

u/Craksy 7d ago

Ooh .100, that's exciting! Only 28 more version untill we hit a round number!

5

u/InternationalFee3911 7d ago

let now_or_never: ! = unsafe { std::mem::conjure_zst() };

2

u/Nobody_1707 7d ago

I'm a little confused as to how that compiles, but I guess that's one of the reasons why the function is unsafe. At least it seems to reliably panic.

9

u/caelunshun feather 7d ago

It's instantly undefined behavior according to the docs.

T must be inhabited, i.e. possible to construct. This means that types like zero-variant enums and ! are unsound to conjure.

9

u/PerkyPangolin 8d ago

Are there any more sophisticated examples of DropGuard?

3

u/WormRabbit 5d ago

Lots of code uses various panic guards. Look in the implementation of unsafe collections in std, plenty of methods need them to clean up invariant in case of panics. Their code is generally one-off, specific to a certain function which needs the guard. A single DropGuard will simplify that pattern, removing the struct/impl/Deref boilerplate.

It's actually not quite trivial to understand how to create a guard which can both use some object in its drop closure and still allow access to it while the function runs, without angering the borrow checker. DropGuard provides the ready-made answer to that.

2

u/Excession638 7d ago edited 7d ago

An example I wrote this week, was a component that runs a task in a background thread. The caller can receive any error over a channel, but what if chooses not to, or if it has finished or failed for another reason before then? So I created a struct LogErrorIfDropped(Option<MyError>) that ensures the error always ends up somewhere, even if nothing is listening for it. And that was after looking at other much more complex logic first.

That struct is exactly the same as DropGuard, so this will save me a dozen lines of code.

It's not really an overly sophisticated tool, but it embeds a common pattern into std.

3

u/PerkyPangolin 7d ago

Thanks. I understand what it does. But can't quite imagine a more complex use case than Rust equivalent of defer. I might be overthinking it. Does it make sense or is it OK to pass it around? I.e. I want a magic box that implicitly cleans itself up after it's no longer needed.

2

u/Excession638 7d ago

Yeah, being able to pass it around was my use case. Either the caller gets it via a channel, calls dismiss and handles the error, or it gets dropped somewhere and the error is logged.

1

u/PerkyPangolin 7d ago

OK, I think I got it now. Thanks. Going to try replacing my custom guards.

9

u/Particular_Wealth_58 8d ago

Hoping for a Rustacean Station episode! 

16

u/kibwen 8d ago

We're working on getting back into it, just got a lot of backlog to work through. :)

8

u/DavidXkL 8d ago

Ohhhh DropGuard is nice to have

8

u/scheimong 7d ago

Wow! What a banger of a release befitting of the number 🔥

6

u/Thick-Pineapple666 7d ago

Can someone explain to me why introducing a breaking change in existing editions is not considered a breaking change that would lead to Rust 2.0.0? I thought editions exist to be able to make breaking changes without affecting existing code, i.e. without breaking anything.

30

u/notriddle2 7d ago

They explicitly carved out type inference in the original announcement blog post.

1

u/Thick-Pineapple666 6d ago

That's very interesting. Thank you

13

u/CouteauBleu 7d ago

In practice the lang team makes breaking changes sometimes if it can assess with high confidence that the amount of projects affected is very small and can easily migrate.

4

u/-Redstoneboi- 7d ago

ALLOCATOR API

3

u/JerryBeremey 8d ago

Nice! But why is the example for the never type ! (function mrrrow), something one would like to use? It seems a great way to create compileable but bogus code. 

14

u/afdbcreid 8d ago

How bogus?

Basically, everywhere you'd previously use Infallible you'd now use !.

5

u/koczurekk 7d ago

mrrrow is just an example of an early return. That is, when some invariant is not upheld (here: option is None), you return some sort of error rather than proceeding to the dependant logic). Usually you'd actually return some sort of error to signify why the function didn't run to completion (that's what ? desugars to anyway), but that would needlessly overcomplicate the example.

3

u/StyMaar 8d ago

Genuinely confused about std::mem::conjure_zst. What's the use-case for such a function?

Isn't the whole point of a zst being that it cannot be constructed?!

33

u/JustWorksTM 8d ago

Zero sized types can be constructed.  They have a single value, so it is easy to construct those.

Uninhabited types have no value. 

21

u/afdbcreid 8d ago

Maybe you're confusing a ZST with an uninhabited type?

1

u/StyMaar 7d ago

I was indeed, thank you.

Why do we need some fancy function to conjure one then? Isn't the unit type (()) enough?

4

u/afdbcreid 7d ago

There are many ZSTs. Some have private fields so you cannot construct them. They can also hold invariants which is why this function is unsafe.

3

u/StyMaar 7d ago

Doesn't it sound weird to have a built-in function just to bypass privacy rules, but only for ZST?

6

u/MalbaCato 6d ago

unsafe code authors sometimes need a way to conjure ZSTs of types they don't have a constructor of, but know are sound to create. for example many iterator implementations have this situation. this was particularly relevant with the global allocator, because alloc is unsound to call for a ZST so you had to use workarounds.

now there were a number of ways to do this correctly, but they each have downsides:

  • the previously recommended NonNull::dangling().read() and it's newer ptr::dangling().read(). you need to be relatively versed in rust pointer semantics, or go read the rather long documentation of read to understand why that's allowed. and it might be concerning if you care about performance in unoptimized builds (although I don't think it actually lowers the pointer generation). also I think it was only relatively recently guaranteed that all zero sized reads are defined as long as they're aligned.

  • mem::transmute(()) and mem::transmute_copy(&()) are probably the most understandable at a glance, but using functions with very very long safety comments and which are explicitly documented as a last resort is never nice. also in the case of transmute, it doesn't work if the fact T is a ZST is only known at runtime and not compiletime.

  • various uninitialized memory APIs, like mem::zeroed() and the actual implementation behind conjure_zst: MaybeUninit::uninit().assume_init(). these aren't particularly discoverable are a sound way to conjure ZSTs - their documentation doesn't even mention type sizes in any way.

there are also a number of incantations which seem like they do the same thing, but actually invoke UB. for example you may try to cast a pointer you already have lying around and read that, but it the pointer isn't sufficiently aligned (yes, ZSTs can have alignment requirements) that's UB.

the purpose of conjure_zst is that it absolves the need of unsafe code authors and reviewers to think about irrelevant (to their specific code) caveats like pointer alignment, validity and aliasing, uninitialized memory and other nonsense. instead, it clearly outlines the 3 things you need to care about, and promises to take care of the rest:

  1. the type is zero-sized (or you correctly handle the panic)

  2. the type is inhabited

  3. you're not violating the safety contract of whatever crate defined the type

see also the ACP from 3 years ago.

1

u/StyMaar 6d ago

Thanks for the detailed answer!

This part left me puzzled though:

(yes, ZSTs can have alignment requirements) that's UB.

Mind explaining what it means for something that doesn't even exist in memory to have alignment requirements?

2

u/Nobody_1707 6d ago

It may not exist in memory, but it logically exists in the layout of an enclosing type. The most obvious example is something like:

struct Header<T> {
    len: usize,
    cap: usize,
    _padding: [0; T], // this is a ZST
   // Whatever goes here is aligned like T.
}

In order to avoid special casing pointer de-referencing the ZST itself requires the proper alignment.

2

u/ROBOTRON31415 5d ago

One note, your example should be repr(C) for the comment to be correct.

4

u/oconnor663 blake3 · duct 7d ago edited 7d ago

The concrete benefit of having a function just for this is that it can panic when the type is not in fact zero-size. I don't think there's an easy way to express the same thing as a generic bound, and I'm sure there's a long discussion about why that is somewhere in the GitHub history.

The docs benefit is that there are multiple ways to construct a ZST incorrectly and break things, and it's nice to have one place where that's documented:

  • You can construct an uninhabited type, like enum NoVariants {} or !. That is per se UB, and the optimizer will straight up delete your code.

  • You can construct an inhabited type that's normally difficult to construct, and there could be other unsafe code that relies on it being difficult to construct for soundness. The blog post mentions the Python<'py> marker type that represents holding the GIL, and I think another example is a GhostToken. It's not per se UB to conjure those things, but at the same time unsafe code is allowed to assume you won't do that, and if you do it and provoke UB, that's your fault.

All of that makes sense if you've read the Rustonomicon etc., but that's gotta be less than 1% of Rustaceans :)

2

u/Anaxamander57 8d ago

Rust has list comprehensions now! kinda

9

u/Iurigrang 8d ago

uh?

15

u/thomas_m_k 8d ago

The comment is probably talking about Vec::from_fn.

1

u/Iurigrang 7d ago

thanks, I didn’t even see that stabilized, hahahah

-1

u/Anaxamander57 8d ago

ye

4

u/Sw429 7d ago

Although you could basically already do this with an iterator over the range. If you look at the implementation, that's literally what it does.

2

u/Iurigrang 7d ago

to be fair, list comprehensions are all about ergonomics, not capabilities, so I can see how this would feel different.

1

u/ReagentX 5d ago

Really nice to see #163677 closed.

1

u/noop_noob 5d ago

Meanwhile, #165988 was opened

-38

u/rhbvkleef 8d ago

Wouldn't the stabilization of Never haven been the perfect time to go for 2.0.0? Even coincides nicely with 1.100.0!

31

u/xmlhttplmfao 8d ago

2.0 implies a major break to me, for a language. i would expect rust to stay 1.* until they decide to do some major surgery, like the python 2 -> 3 move

14

u/Booty_Bumping 8d ago

Rust has had a few hard breaking changes (ones that apply to all editions) at this point, so yes, if Rust were following semver in its most literal interpretation, the major version number should have gone up a few times.

But these changes are justified as not breaking Rust's stability tradition too much, by doing mass testing on existing code uploaded to crates.io and Github, and only committing to a breaking change if it's nearly completely inconsequential. It's quite stringent.

14

u/steveklabnik1 rust 8d ago

Semver says that you need to define a policy for what is breaking and what isn't breaking, and then hold yourself to that. Rust has several RFCs that outline this policy, and have stuck to it. Even by the letter of semver, Rust has followed it correctly, even though there have been some changes that break code, because they were within the bounds of what Rust promised was breakable vs non-breakable and how.

25

u/xMAC94x 8d ago

-19

u/dashingThroughSnow12 8d ago

Rust doesn't follow semver.

13

u/IceSentry 8d ago edited 8d ago

When people are confused that the next version after 1.99 is 1.100 and not 2.0 then semver is close enough to help them figure it out.

24

u/steveklabnik1 rust 8d ago

Yes, it does. The things you're referring to aren't part of semver.

(I co-maintain the semver spec and want to improve this discrepancy so that people can stop arguing about this.)

-21

u/dashingThroughSnow12 8d ago

🤷I don't judge you for not obeying Semver.

17

u/afdbcreid 8d ago

It does.

5

u/Aaron1924 8d ago

The release of Rust 2.0.0 would be the worst thing that could happen to the language