Series: The Rust Annals
Vol. I Issue 36 nlopes.dev
Announcing Rust 1.35.0
Rust 1.35.0’s highlight is direct support for boxed FnOnce, FnMut, and Fn trait objects, removing the need for FnBox and temporary forwarding closures. Non-capturing closures can also be coerced to unsafe function pointers, while argument-free dbg!(), new standard-library APIs, and more precise Clippy lints improve everyday development.
Boxed closure trait objects can be passed and called directly
The release’s highlight is that FnOnce, FnMut, and Fn are now implemented for the corresponding boxed trait objects. Previously, boxed closures did not implement their respective function traits: calling one required FnBox, and passing one to code expecting a function-trait implementor required a temporary closure.
The compiler limitation behind this restriction was fixed with the introduction of unsized locals. A boxed function can consequently be supplied directly where a function trait is expected:
fn foo(x: Box<dyn Fn(u8) -> u8>) -> Vec<u8> {
vec![1, 2, 3, 4].into_iter().map(x).collect()
}
A boxed one-shot closure can also be called directly:
fn foo(x: Box<dyn FnOnce()>) {
x()
}
Non-capturing closures can coerce to unsafe function pointers
Since Rust 1.19.0, closures that capture nothing from their environment could be coerced to ordinary function pointers:
fn twice(x: u8, f: fn(u8) -> u8) -> u8 {
f(f(x))
}
fn main() {
assert_eq!(42, twice(0, |x| x + 21));
}
Rust 1.35.0 extends that coercion to unsafe fn pointers:
/// The safety invariants are those of the `unsafe fn` pointer passed.
unsafe fn call_unsafe_fn_ptr(f: unsafe fn()) {
f()
}
fn main() {
// SAFETY: There are no invariants.
// The closure is statically prevented from doing unsafe things.
unsafe {
call_unsafe_fn_ptr(|| {
dbg!();
});
}
}
Argument-free dbg!() traces control flow
The dbg! macro, introduced in Rust 1.32.0, prints an expression’s value together with source context:
fn main() {
let mut x = 0;
if dbg!(x == 1) {
x += 1;
}
dbg!(x);
}
[src/main.rs:4] x == 1 = false
[src/main.rs:8] x = 0
It can now be called without an argument, which is useful for tracing which branches an application takes:
fn main() {
let condition = true;
if condition {
dbg!();
}
}
[src/main.rs:5]
Library APIs and Clippy make common intent more explicit
The standard library adds methods for copying one floating-point number’s sign onto another:
fn main() {
assert_eq!(3.5_f32.copysign(-0.42), -3.5);
}
All five range forms gain a direct membership test:
fn main() {
if (0..=10).contains(&5) {
println!("Five is included in zero to ten.");
}
}
Immutable and mutable RefCell borrow guards can now be mapped and split into multiple borrows for different components. Another new method maps and replaces a cell’s current value through a closure and returns the old value.
A new pointer function hashes a raw pointer’s address rather than the pointed-to value. The new copying operation for both Option<&T> and Option<&mut T> has the same behavior as opt.cloned(), but requires T: Copy. This makes code stop compiling if T ceases to implement Copy, while avoiding the implication that an expensive clone might occur.
Clippy adds the drop_bounds lint for generic bounds such as:
fn foo<T: Drop>(x: T) {}
A T: Drop bound is almost always a mistake: it excludes types such as u8 that have trivial drop glue, yet does not account for types such as String whose interesting destructor behavior comes from embedded types such as Vec<u8>. Clippy also split redundant_closure into redundant_closure and redundant_closure_for_method_calls. The source provides links to the detailed Clippy notes and Cargo notes.