Latest in branch 1.46
1.46.0
Released 27 Aug 2020
(5 years ago)
SoftwareRust
Version1.46
Initial release1.46.0
27 Aug 2020
(5 years ago)
Latest release1.46.0
27 Aug 2020
(5 years ago)
Support status08 Oct 2020
(Ended 5 years, 8 months ago)
Release noteshttps://github.com/rust-lang/rust/releases/tag/1.46.0
Source codehttps://github.com/rust-lang/rust/tree/1.46.0
Downloadhttps://github.com/rust-lang/rust/releases/tag/1.46.0
Rust 1.46 ReleasesView full list

What Is New in Rust 1.46

CategoryHighlights
New FeaturesControl-flow in const fn, #[track_caller] attribute, std::mem::forget const fn, two new std APIs
ImprovementsBetter compile-time computation, more informative panic locations

Can I use control-flow statements inside const fn in Rust 1.46?

Yes, Rust 1.46 expands const fn to support if, if let, match, while, while let, loop, and the logical && and || operators.

This matters if you need to perform branching or iteration at compile time, for example when building lookup tables or compile-time parsers.

  • if / if let
  • match
  • while / while let
  • loop
  • Logical && and ||
const fn compute_sum(limit: usize) -> usize {
    let mut sum = 0;
    let mut i = 0;
    while i < limit {
        sum += i;
        i += 1;
    }
    sum
}

In practice you can also cast arrays to slices inside const fn, enabling more flexible data handling.

const fn slice_example() {
    let arr = [1, 2, 3, 4, 5];
    let s: &[_] = &arr; // cast to slice
}

How does #[track_caller] improve panic messages in Rust 1.46?

#[track_caller] makes panic locations point to the caller of a function that panics, rather than the panic site itself.

This is useful for library authors who want end-users to see where their code triggered the panic.

#[track_caller]
pub fn unwrap(self) -> T {
    match self {
        Some(v) => v,
        None => panic!("called `Option::unwrap()` on a `None` value"),
    }
}

If you implement a custom panic hook, you can retrieve the caller location via std::panic::Location::caller().

Which standard library functions became const in Rust 1.46?

In Rust 1.46, std::mem::forget is now a const fn, and two additional APIs were stabilized as const (the release notes list them without naming).

This allows you to deliberately leak memory or perform other unsafe actions in a const context, which can be handy for compile-time resource management.

const fn leak(value: T) {
    unsafe { std::mem::forget(value) };
}

What practical benefits do the new const fn capabilities bring?

The new const fn features let you perform non-trivial calculations at compile time, reducing runtime overhead.

For example, the const-sha1 crate can now compute SHA-1 hashes during compilation, which gave Microsoft's WinRT bindings a 40× speedup.

Most teams will see faster build times for code that previously required runtime initialization, and tighter guarantees about constant data.

Frequently Asked Questions

Does Rust 1.46 require any changes to existing Cargo.toml files?
No, the update is source-compatible and does not require modifications to Cargo.toml.

Can I use #[track_caller] on methods defined in my own crates?
Yes, you can annotate any function or method that may panic to get the caller location in the panic message.

Are there any breaking changes introduced in Rust 1.46?
No breaking changes were introduced; the release focuses on new features and improvements.

How do I enable the new const fn control flow in my code?
Simply write normal if, match, or loop statements inside a const fn; the compiler now accepts them.

What is one real-world library that benefited from Rust 1.46's const fn enhancements?
The const-sha1 crate can now compute SHA-1 hashes at compile time, leading to a 40-fold speedup in some bindings.

Is std::mem::forget now usable in const contexts?
Yes, you can call std::mem::forget inside a const fn as it is now a const fn.

Releases In Branch 1.46

VersionRelease date
1.46.027 Aug 2020
(5 years ago)