An attribute attaches metadata to a Rust item and can influence compiler behavior. Attributes can relax warnings, prescribe an enum’s representation, or enable platform-specific compilation. section 3.1 section 7.8 section 11.5.2
Attributes shown here control different parts of compilation:
- `#![allow(unused_variables)]` relaxes an unused-variable warning; `#[allow(dead_code)]` applies warning suppression to a function. section 3.1
- The `cfg` attribute compiles platform-specific code. The `repr` attribute can prescribe an enum’s integer representation, trading flexibility and space optimizations for control. section 7.8 section 11.5.2
Listing 3.1 Using type aliases to stub out a type
#![allow(unused_variables)] #1
type File = String; #2
fn open(f: &mut File) -> bool {
true #3
fn close(f: &mut File) -> bool {
true #3
}
#[allow(dead_code)] #4
fn read(f: &mut File,
save_to: &mut Vec<u8>) -> ! { #5
unimplemented!() #6
}
fn main() {
let mut f1 = File::from("f1.txt"); #7
open(&mut f1);
//read(f1, vec![]); #8
close(&mut f1);
}
See warning-suppression attributes used in a small file-handling stub. section 3.1

























