Rust FAQ
Introduction
Rust is a modern programming language designed to be safe, fast, and reliable. It’s great for building software where performance matters, like web browsers, games, or even operating systems, without risking crashes or security issues. Rust makes sure your code handles memory safely without needing a garbage collector, which is a big deal for systems programming. This FAQ is here to answer common questions about Rust in simple, easy-to-understand language, whether you’re a beginner or an experienced coder curious about Rust.
Table of Contents
This book is split into sections to help you find answers quickly. Each section covers a different part of Rust, from basics like how to write code to advanced topics like memory management and traits. Here’s what’s included:
PART01 -- Introduction and Table of Contents
- Introduction
- Table of Contents
- Nomenclature and Common Abbreviations
PART02 -- Environmental/Managerial Issues
- Q1: What is Rust? What is systems programming?
- Q2: What are some advantages of Rust?
- Q3: Who uses Rust?
- Q4: Does Rust run on machine
Xrunning operating systemY? - Q5: What Rust compilers are available?
- Q6: Is there a translator that turns Rust code into C code?
- Q7: Are there any Rust standardization efforts underway?
- Q8: Where can I find the latest Rust language specification?
- Q9: Is Rust backward compatible with C?
- Q10: What books are available for Rust?
- Q11: How long does it take to learn Rust?
PART03 -- Basics of the Paradigm
- Q12: What is a struct?
- Q13: What is an enum?
- Q14: What is a reference in Rust?
- Q15: What happens if you assign to a reference?
- Q16: How can you rebind a reference to a different value?
- Q17: When should I use references, and when should I use owned types?
- Q18: What are functions? What are their advantages? How are they declared?
PART04 -- Constructors and Destructors
- Q19: What is a constructor in Rust? Why would I use one?
- Q20: What is the
Droptrait for? Why would I use it?
PART05 -- Operator Overloading
- Q21: What is operator overloading in Rust?
- Q22: What operators can/cannot be overloaded?
- Q23: Can I create a
**operator forto-the-power-ofoperations?
PART06 -- Traits
- Q24: What is a trait?
- Q25: Do traits violate encapsulation?
- Q26: What are some advantages/disadvantages of using traits?
- Q27: What does it mean that trait implementation is neither inherited nor transitive?
- Q28: When would I use a method versus a free function?
PART07 -- Input/Output
- Q29: How can I provide printing for a
struct X? - Q30: Why should I use
std::ioinstead of C-style I/O? - Q31: Why use
format!orprintln!instead of C-styleprintf?
PART08 -- Memory Management
- Q32: Does
dropdestroy the reference or the referenced data? - Q33: Can I use C's
free()on pointers allocated with Rust'sBox? - Q34: Why should I use
BoxorRcinstead of C'smalloc()? - Q35: Why doesn't Rust have a
realloc()equivalent? - Q36: How do I allocate/unallocate an array in Rust?
- Q37: What happens if I forget to use
Vecwhen deallocating an array? - Q38: What's the best way to define a
NULL-like constant in Rust?
PART09 -- Debugging and Error Handling
- Q39: How can I handle errors in a constructor-like function?
- Q40: How can I compile out debugging print statements?
PART10 -- Ownership and Borrowing
- Q41: What is ownership in Rust?
- Q42: Is ownership a good goal for system design?
- Q43: Is managing ownership tedious?
- Q44: Should I aim for ownership correctness early or later in development?
- Q45: What is a mutable borrow?
- Q46: What are immutable and mutable borrows?
- Q47: What is
unsafecode, and when is it necessary? - Q48: Does bypassing borrow checking mean losing safety guarantees?
PART11 -- Traits and Inheritance
- Q49: What is trait-based inheritance?
- Q50: How does Rust express inheritance-like behavior?
- Q51: How do you implement traits in Rust?
- Q52: What is
compositional programmingin Rust? - Q53: Should I cast from a type implementing a trait to the trait object?
- Q54: Why doesn't casting from
Vec<Derived>toVec<Trait>work? - Q55: Does
Vec<Derived>not being aVec<Trait>mean vectors are problematic?
PART12 Traits -- Dynamic Dispatch
- Q56: What is a
dyn Trait? - Q57: What is dynamic dispatch? Static dispatch?
- Q58: Can I override a non-dynamically dispatched method?
- Q59: Why do I get a warning about method hiding in trait implementations?
PART13 Traits -- Conformance
- Q60: Can I restrict access to trait methods in implementing types?
- Q61: Is a
Circlea kind of anEllipsein Rust? - Q62: Are there solutions to the
Circle/Ellipseproblem in Rust?
PART14 Traits -- Access Rules
- Q63: Why can't I access private fields of a struct implementing a trait?
- Q64: What's the difference between
pub,pub(crate), and private visibility? - Q65: How can I protect structs from breaking when internal fields change?
PART15 Traits -- Construction and Destruction
- Q66: Why does a trait's default method get called instead of the implementor's?
- Q67: Does a struct's
Dropimplementation need to call a trait'sDrop?
PART16 Traits -- Composition vs. Inheritance
- Q68: How do you express composition in Rust?
- Q69: How are composition and trait implementation similar/dissimilar?
- Q70: Should I cast from a trait object to a supertrait?
- Q71: Should I cast from a struct to a trait it implements?
- Q72: What are the visibility rules with traits and impls?
- Q73: Do most Rust programmers use composition or traits for code reuse?
PART17 -- Abstraction
- Q74: Why is separating interface from implementation important?
- Q75: How do I separate interface from implementation in Rust?
- Q76: What is a trait object?
- Q77: What is a
dyntrait method? - Q78: How can I provide printing for an entire trait hierarchy?
- Q79: What is a custom
Dropimplementation? - Q80: What is a factory function in Rust?
PART18 -- Style Guidelines
- Q81: What are some good Rust coding standards?
- Q82: Are coding standards necessary? Sufficient?
- Q83: Should our organization base Rust standards on C++ experience?
- Q84: Should I declare variables in the middle of a function or at the top?
- Q85: What file-name convention is best?
foo.rs?foo.rust? - Q86: What module naming convention is best?
foo_mod.rs?foo.rs? - Q87: Are there any clippy-like guidelines for Rust?
PART19 -- Rust vs. Other Languages
- Q88: Why compare Rust to other languages? Is this language bashing?
- Q89: What's the difference between Rust and C++?
- Q90: What is
zero-cost abstraction, and how does it compare to other languages? - Q91: Which is a better fit for Rust: static typing or dynamic typing?
- Q92: How can you tell if you have a dynamically typed Rust library?
- Q93: Will Rust include dynamic typing primitives in the future?
- Q94: How do you use traits in Rust compared to interfaces in other languages?
- Q95: What are the practical consequences of Rust's trait system vs. other languages?
- Q96: Do you need to learn another systems language before Rust?
- Q97: What is
std? Where can I get more info about it?
PART20 -- Ownership and Borrowing Semantics
- Q98: What are ownership and borrowing semantics, and which is best in Rust?
- Q99: What is
Boxdata, and how/why would I use it? - Q100: What's the difference between
BoxandRc/Arc? - Q101: Should struct fields be
Boxor owned types? - Q102: What are the performance costs of
Boxvs. owned types? - Q103: Can methods be inlined with dynamic dispatch?
- Q104: Should I avoid borrowing semantics entirely?
- Q105: Does borrowing's complexity mean I should always use owned types?
PART21 -- Linkage to/Relationship with C
- Q106: How can I call a C function from Rust code?
- Q107: How can I create a Rust function callable from C code?
- Q108: Why do I get linker errors when calling C/Rust functions cross-language?
- Q109: How can I pass a Rust struct to/from a C function?
- Q110: Can my C function access data in a Rust struct?
- Q111: Why do I feel further from the machine in Rust compared to C?
PART22 -- Function Pointers and Closures
- Q112: What is the type of a function pointer? Is it different from a closure?
- Q113: How can I ensure Rust structs are only created with
Box? - Q114: How do I pass a closure to a C callback or event handler?
- Q115: Why am I having trouble taking the address of a Rust function?
- Q116: How do I declare an array of function pointers or closures?
PART23 -- Collections and Generics
- Q117: How can I insert/access/change elements in a
Vec/HashMap/etc? - Q118: What's the idea behind generics in Rust?
- Q119: What's the syntax/semantics for a generic function?
- Q120: What's the syntax/semantics for a generic struct?
- Q121: What is a generic type?
- Q122: What is
monomorphization? - Q123: How can I emulate generics without full compiler support?
PART24 -- Nuances of Particular Implementations
- Q124: Why don't variadic functions work in Rust?
- Q125: Why do Rust binaries seem large for small programs?
- Q126: Is there a parser generator for Rust grammar?
- Q127: What is Rust 1.0? 1.XX? 2021 Edition?
- Q128: How does the 2021 Edition differ from earlier Rust versions?
- Q129: Why were certain features prioritized over others in Rust?
- Q130: What was Rust pre-1.0, and how does it differ from current Rust?
PART25 -- Miscellaneous Technical Issues
- Q131: Why do structs with static data cause linker errors?
- Q132: What's the difference between
structandenumin Rust? - Q133: Why can't I overload a function by its return type?
- Q134: What is
persistencein Rust? What is apersistent object?
PART26 -- Miscellaneous Environmental Issues
- Q135: Is there a LaTeX macro for proper Rust formatting?
- Q136: Where can I find a pretty printer for Rust source code?
- Q137: Is there a Rust-mode for GNU Emacs? Where can I get it?
- Q138: What is
cargo? - Q139: Where can I get platform-specific answers (e.g., Windows, Linux)?
- Q140: Why does my Rust program report floating-point issues?
Nomenclature and Common Abbreviations
Rust has some terms and abbreviations that might be new. Here’s a quick guide to help you understand them:
- Rust: The programming language itself, created by Mozilla and now maintained by the Rust Foundation.
- Cargo: Rust’s built-in tool for managing projects, building code, and downloading libraries (called “crates”).
- Crate: A package of Rust code, like a library or program, that you can share or use.
- Struct: A way to define custom data types, like a blueprint for your data.
- Enum: A type that can be one of several variants, great for modeling choices or states.
- Trait: A way to define shared behavior for different types, like an interface in other languages.
- Box: A smart pointer for storing data on the heap (memory Rust manages for you).
- Rc/Arc: Tools for sharing data between multiple parts of your program (Reference Counting/Atomic Reference Counting).
- Borrow: Temporarily using data without owning it, following Rust’s strict rules to stay safe.
- Lifetime: A concept in Rust to ensure references are valid for the right amount of time.
- std: Short for “standard library,” the collection of built-in Rust tools and functions.
- FFI: Foreign Function Interface, how Rust talks to code written in other languages like C.