Rust Attributes Quiz

Rust
0 Passed
0% acceptance

Master Rust's attribute system for conditional compilation, testing, documentation, and code generation

40 Questions
~80 minutes
1

Question 1

What is an attribute in Rust?

A
Metadata that provides information to compiler about code processing
B
Function annotation
C
Variable type
D
Attributes don't exist
2

Question 2

What is the syntax for attributes?

A
#[attribute_name] or #[attribute_name = "value"] or #[attribute_name(key = "value")]
B
@attribute_name
C
// attribute_name
D
No specific syntax
3

Question 3

What does #[derive(Debug)] do?

A
Automatically implements Debug trait for struct/enum
B
Makes struct debuggable
C
Adds debugging information
D
derive doesn't exist
4

Question 4

What is the difference between inner and outer attributes?

A
#![inner] applies to enclosing item, #[outer] applies to following item
B
They are identical
C
Inner attributes are deprecated
D
No difference
5

Question 5

What does #[allow(unused_variables)] do?

A
Suppresses unused variable warnings for annotated item
B
Deletes unused variables
C
Makes variables used
D
allow doesn't exist
6

Question 6

What is #[cfg(test)] used for?

A
Conditional compilation - code only compiled when running tests
B
Test configuration
C
Test execution
D
cfg doesn't exist
7

Question 7

What does #[test] do?

A
Marks function as unit test, run with cargo test
B
Makes function testable
C
Debugs function
D
test doesn't exist
8

Question 8

What is the difference between #[test] and #[bench]?

A
#[test] for unit tests, #[bench] for benchmarks (deprecated)
B
They are identical
C
#[bench] is better
D
No difference
9

Question 9

What does #[should_panic] do?

A
Test passes if function panics, fails if doesn't panic
B
Makes test panic
C
Ignores panics
D
should_panic doesn't exist
10

Question 10

What is #[ignore] used for?

A
Skips test unless explicitly run with --ignored
B
Deletes test
C
Makes test faster
D
ignore doesn't exist
11

Question 11

What does #[doc = "documentation"] do?

A
Adds documentation to item, appears in rustdoc
B
Prints documentation
C
Generates docs
D
doc doesn't exist
12

Question 12

What is #[doc(hidden)]?

A
Hides item from public documentation
B
Makes item invisible
C
Deletes documentation
D
hidden doesn't exist
13

Question 13

What does #[inline] do?

A
Suggests compiler to inline function at call sites
B
Forces inlining
C
Prevents inlining
D
inline doesn't exist
14

Question 14

What is #[cold]?

A
Marks function as unlikely to be called, affects code layout
B
Makes function slow
C
Optimizes for cold paths
D
cold doesn't exist
15

Question 15

What does #[no_mangle] do?

A
Prevents Rust from mangling function name for linking
B
Removes function
C
Changes function name
D
no_mangle doesn't exist
16

Question 16

What is #[repr(C)]?

A
Uses C-compatible memory layout for struct
B
Makes struct C code
C
Changes struct syntax
D
repr doesn't exist
17

Question 17

What is #[repr(transparent)]?

A
Single-field struct has same layout as its field
B
Makes struct invisible
C
Changes transparency
D
transparent doesn't exist
18

Question 18

What does #[non_exhaustive] do?

A
Prevents external crates from exhaustive matching on enum/struct
B
Makes enum incomplete
C
Limits enum usage
D
non_exhaustive doesn't exist
19

Question 19

What is #[deprecated]?

A
Marks item as deprecated, shows warning when used
B
Deletes item
C
Makes item old
D
deprecated doesn't exist
20

Question 20

What does #[must_use] do?

A
Warning if function result not used
B
Forces result usage
C
Makes result required
D
must_use doesn't exist
21

Question 21

What is conditional compilation?

A
Compiling different code based on target platform, features, etc.
B
Compiling sometimes
C
Conditional execution
D
No conditional compilation
22

Question 22

What does #[cfg(target_os = "linux")] do?

A
Code only compiled on Linux systems
B
Changes target OS
C
Detects OS at runtime
D
cfg doesn't work with target_os
23

Question 23

What is feature flags in Cargo?

A
Optional functionality enabled with --features flag
B
Cargo features
C
Compilation flags
D
No feature flags
24

Question 24

What does #[cfg_attr(condition, attribute)] do?

A
Conditionally applies attribute based on cfg condition
B
Changes attributes
C
Combines conditions
D
cfg_attr doesn't exist
25

Question 25

What is #[proc_macro] used for?

A
Defines procedural macro function
B
Processes macros
C
Creates macros
D
proc_macro doesn't exist
26

Question 26

What is the difference between #[proc_macro] and #[proc_macro_derive]?

A
#[proc_macro] for function-like macros, #[proc_macro_derive] for custom derives
B
They are identical
C
proc_macro_derive is deprecated
D
No difference
27

Question 27

What does #[macro_export] do?

A
Makes macro available to other crates
B
Exports macro
C
Makes macro global
D
macro_export doesn't exist
28

Question 28

What is attribute precedence?

A
Inner attributes override outer, tool attributes have lowest precedence
B
No precedence rules
C
Random precedence
D
Precedence doesn't exist
29

Question 29

What are tool attributes?

A
#[rustfmt::skip] - attributes for external tools
B
Tool helpers
C
Build tools
D
Tool attributes don't exist
30

Question 30

What does #[rustfmt::skip] do?

A
Tells rustfmt to skip formatting this item
B
Skips Rust formatting
C
Changes formatting
D
rustfmt doesn't exist
31

Question 31

What is #[allow(clippy::pedantic)]?

A
Allows all pedantic clippy lints for annotated item
B
Makes code pedantic
C
Changes clippy behavior
D
clippy doesn't exist
32

Question 32

What does #[warn(missing_docs)] do?

A
Makes missing documentation a warning
B
Adds documentation
C
Changes warning level
D
warn doesn't exist
33

Question 33

What is #[deny(unsafe_code)]?

A
Makes unsafe code a compilation error
B
Allows unsafe code
C
Changes unsafe behavior
D
deny doesn't exist
34

Question 34

What does #[forbid(unused_must_use)] do?

A
Makes ignoring #[must_use] results a compilation error
B
Allows ignoring results
C
Changes must_use behavior
D
forbid doesn't exist
35

Question 35

What is #[link_name = "c_function"]?

A
Links Rust function to C function with different name
B
Changes function name
C
Creates link
D
link_name doesn't exist
36

Question 36

What does #[export_name = "my_func"] do?

A
Exports function with specific name in symbol table
B
Changes function name
C
Makes function exportable
D
export_name doesn't exist
37

Question 37

What is #[used]?

A
Prevents linker from removing seemingly unused static/item
B
Marks as used
C
Changes usage
D
used doesn't exist
38

Question 38

What does #[path = "custom.rs"] do?

A
Specifies custom file path for module
B
Changes module path
C
Creates path
D
path doesn't exist
39

Question 39

What is attribute macro expansion order?

A
Derive macros expand first, then attribute macros, then function-like
B
Random order
C
No specific order
D
Order doesn't matter
40

Question 40

In a cross-platform library with optional features, comprehensive tests, and custom derive macros, which attributes would you use and why?

A
#[cfg] for platform-specific code, #[test] for testing, #[proc_macro_derive] for code generation, #[allow] for necessary warnings
B
Only #[derive] attributes
C
No attributes needed
D
Only built-in attributes

QUIZZES IN Rust