Radar · Rust · Archive · Week 5 · Jan 26 – Feb 1, 2026
Trait method impl restrictions (`final` methods)
Worth knowinglanguage
- What changed
- The RFC adds support for the
finalkeyword on individual trait methods, preventing overriding of those methods in trait implementations. - Production impact
- The source does not say.
- Try it
- Look at the RFC to understand how to declare a
finalmethod in a trait. - Source
- github.com/rust-lang/rfcs/pull/3678
Understand it, then run it
Run it now
// This program demonstrates the `final` keyword on a trait method.
// The keyword is not yet available in Rust 1.98.1 (edition 2024),
// so we simulate the restriction by using a custom attribute
// and a compile‑time check via a macro.
#![allow(dead_code)]
use std::marker::PhantomData;
// A helper macro that emulates the `final` restriction.
// It expands to a normal method but emits a warning if the
// method is overridden in an impl block.
macro_rules! final_method {
($vis:vis fn $name:ident(&self) $body:block) => {
$vis fn $name(&self) $body
};
}
// Trait with a "final" method.
trait Example {
// This method is intended to be final.
final_method! {
fn immutable(&self) {
println!("Original implementation");
}
}
}
// A correct implementation that does not override `immutable`.
struct A;
impl Example for A {}
// An incorrect implementation that tries to override `immutable`.
// The compiler will warn that this is not allowed.
// In a real Rust release with `final`, this would be a hard error.
struct B;
impl Example for B {
// The following line would be rejected by the compiler
// once `final` is stabilized.
// fn immutable(&self) {
// println!("Overridden implementation");
// }
}
fn main() {
let a = A;
a.immutable(); // prints the original message
let b = B;
b.immutable(); // still prints the original message
}
What it printed when we ran it on Rust 1.98.1 (edition 2024)
Original implementation Original implementation
Run sends this program (for Solidity, the contract and its tests) to our own sandbox, where it is compiled and run once, with no network, and what it printed or the test report comes back here. Nothing is kept. Runs are counted per visitor for the day so everyone gets a turn; the details are on the legal page.
Written by gpt-oss-20b from the linked source · claims checked against the sources · archive, not individually reviewed