{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/rust-www/pulls/comments/45450708","id":45450708,"diff_hunk":"@@ -0,0 +1,1028 @@\n+---\n+layout: faq\n+title: Frequently Asked Questions &middot; The Rust Programming Language\n+---\n+\n+# Frequently Asked Questions\n+\n+This page exists to answer common questions about the Rust programming language. It is not a complete guide to the language, nor is it a tool for teaching the language. It is a reference to answer oft-repeated questions people in the Rust community encounter, and to clarify the reasoning behind some of Rust's design decisions.\n+\n+If there is some common or important question you feel is wrongly left unanswered here, feel free to [help us fix it](https://github.com/rust-lang/rust-www/blob/master/CONTRIBUTING.md).\n+\n+<div id=\"toc\">\n+    <h2>Table of Contents</h2><a href=\"#toggle-toc\"></a>\n+    <ol id=\"toc-contents\">\n+        <li><a href=\"#performance\">Performance</a></li>\n+        <li><a href=\"#syntax\">Syntax</a></li>\n+        <li><a href=\"#numerics\">Numerics</a></li>\n+        <li><a href=\"#strings\">Strings</a></li>\n+        <li><a href=\"#collections\">Collections</a></li>\n+        <li><a href=\"#ownership\">Ownership</a></li>\n+        <li><a href=\"#lifetimes\">Lifetimes</a></li>\n+        <li><a href=\"#generics\">Generics</a></li>\n+        <li><a href=\"#input-output\">Input / Output</a></li>\n+        <li><a href=\"#error-handling\">Error Handling</a></li>\n+        <li><a href=\"#concurrency\">Concurrency</a></li>\n+        <li><a href=\"#macros\">Macros</a></li>\n+        <li><a href=\"#debugging\">Debugging</a></li>\n+        <li><a href=\"#low-level\">Low-Level</a></li>\n+        <li><a href=\"#cross-platform\">Cross-Platform</a></li>\n+        <li><a href=\"#modules-and-crates\">Modules and Crates</a></li>\n+        <li><a href=\"#libraries\">Libraries</a></li>\n+        <li><a href=\"#design-patterns\">Design Patterns</a></li>\n+        <li><a href=\"#other-languages\">Other Languages</a></li>\n+        <li><a href=\"#documentation\">Documentation</a></li>\n+        <li><a href=\"#project\">Project</a></li>\n+\n+    </ol>\n+</div>\n+\n+<h2 id=\"performance\">Performance</h2>\n+\n+### How fast is Rust?\n+\n+Fast! Rust is already competitive with idiomatic C and C++ in a number of benchmarks.\n+\n+It is an explicit goal of Rust to be at least as fast as C++. Language decisions are made with performance in mind, and given that Rust is built on LLVM, any LLVM performance improvements also help Rust.\n+\n+### Is Rust garbage collected?\n+\n+No. A language that requires a GC is a language that opts into a larger, more complex runtime than Rust cares for. Rust is usable on bare metal with no extra runtime.\n+\n+Additionally, garbage collection is frequently a source of non-deterministic behavior. Rust provides the tools to make using a GC [possible and even pleasant](http://manishearth.github.io/blog/2015/09/01/designing-a-gc-in-rust/), but it is not part of the language as provided.\n+\n+### Why is my program slow?\n+\n+If you compiled with Cargo, did you use the `--release` flag? If you compiled with `rustc` directly, did you use the `-O` flag? Either of these will compile with optimizations turned on. The Rust languages uses a lot of optimizations to create efficient machine code, but you need to explicitly ask for them, as they also result in longer compilation times that may be undesirable during development.\n+\n+### Why is Rust compilation slow?\n+\n+Most of the compilation time is spent in type checking and code translation, with some time also used for safety checks. All of these are required for the strong guarantees Rust makes, but they require some time to run. Add in optimizations, and Rust's compilation is slower than a simpler language that doesn't provide Rust's guarantees.\n+\n+But all is not lost. The Rust compiler has evolved significantly over a number of years, and a lot of good work is being done to make it work faster, and to make it feel faster during development.\n+\n+### Why is Rust's HashMap so slow?\n+\n+By default, Rust's `HashMap` uses the [SipHash](https://131002.net/siphash/) hashing algorithm, which is designed to prevent [hash table collision attacks](http://programmingisterrible.com/post/40620375793/hash-table-denial-of-service-attacks-revisited) while providing [reasonable performance on a variety of workloads](https://www.reddit.com/r/rust/comments/3hw9zf/rust_hasher_comparisons/cub4oh6).\n+\n+While SipHash [demonstrates competitive performance](http://cglab.ca/%7Eabeinges/blah/hash-rs/) in many cases, one case where it is notably slower than other hashing algorithms is with short keys, such as integers. This is why Rust programmers often observe slow performance with `HashMap`. The [FNV hasher](https://crates.io/crates/fnv) is frequently recommended for these cases, but be aware that it does not have the same collision-resistence properties as SipHash.\n+\n+### Why can't I run benchmarks?\n+\n+You can run benchmarks, but only on the nightly channel. Rust's benchmarking mechanism is currently unstable, as the API has not been deemed ready for stabilization. This may change in the future, but until then benchmarking can only be used on nightly.\n+\n+### Does Rust do tail-call optimization?\n+\n+In general, tail-call optimization is [not guaranteed](https://mail.mozilla.org/pipermail/rust-dev/2013-April/003557.html), but may be done in [limited circumstances](http://llvm.org/docs/CodeGenerator.html#sibling-call-optimization). There was a [proposed extension](https://github.com/rust-lang/rfcs/pull/81) that would allow tail-call elimination in certain contexts, but it is currently postponed. The compiler is still free to optimize tail-calls [when it pleases](http://llvm.org/docs/CodeGenerator.html#sibling-call-optimization), however, and the language has a word (`become`) reserved for future explicit tail calls.\n+\n+### Does Rust have a runtime?\n+\n+Rust has a [very small and limited runtime](https://doc.rust-lang.org/std/rt/) providing a heap, backtraces, unwinding, and stack guards. This runtime is comparable to the [C runtime](http://www.embecosm.com/appnotes/ean9/html/ch05s02.html), and allows for the calling of Rust functions from C without setup.\n+\n+<h2 id=\"syntax\">Syntax</h2>\n+\n+### Why curly braces? Why can't everything be like Haskell/Python?\n+\n+Use of curly braces to denote blocks is a common design choice in a variety of programming languages, and Rust's consistency is useful for people already familiar with the style.\n+\n+Curly braces also allow for more flexible syntax for the programmer, a simpler parser in the compiler, and help reduce the possibility of logic mistakes caused by incorrect indentation, like Apple's [goto fail](https://gotofail.com/) bug.\n+\n+### I can leave out parentheses on `if` conditions, why do I have to put brackets around single line blocks? Why is the C style not allowed?\n+\n+Rust does not require parentheses around the conditional for `if`, `else if`, and the like. If C-style bracketless blocks were allowed, there would be no clear delineation between the condition and the body of the block. Requiring braces also eliminates the dangling-else problem, where nested if-else expressions can be to ambiguous.\n+\n+### Why is there no literal syntax for dictionaries?\n+\n+There are two data types in the Rust standard library that fit the definition of a \"dictionary\": `BTreeMap` and `HashMap`, but they have different performance characteristics, and it's not clear which one should be the default for any potential dictionary literal syntax. Rust prefers to make performance costs explicit, and any selection of a default from these two choices may lead to performance surprises that undermine this intention.\n+\n+If you want a more convenient syntax for `BTreeMap` or `HashMap` creation, you can [create a macro](http://stackoverflow.com/questions/27562739/how-do-i-create-a-hashmap-literal/27582993#27582993) to provide it.\n+\n+### When should I use an implicit return?\n+\n+Implicit returns are simply a coding style option, and can be used anywhere they make sense. While early returns require an explicit `return`, any other return can be made implicit according to your preferences or the preferences of your project.\n+\n+### Why aren't function signatures inferred?\n+\n+- Mechanically, it simplifies the inference algorithm, as inference only requires looking at one function at a time.\n+- Mandatory function signatures help enforce interface stability at both the module and crate level.\n+- It improves code comprehension for programmer, eliminating the need for an  IDE running an inference algorithm across an entire crate to be able to guess at a function's argument types; it's always explicit and nearby.\n+\n+### Why does `match` have to be exhaustive?\n+\n+`match` being exhaustive has some useful properties. First, if every possibility is covered by the `match`, adding further variants to an `enum` in the future will cause a compilation failure, rather than an error at runtime. Second, it makes cost explicit. In general, the only safe way to have a non-exhaustive `match` would be to panic the thread if nothing is matched, though it could fall through if the type of the `match` expression is `()`. This sort of hidden cost and special casing is against the language's philosophy. It is easy to ignore all unspecified cases by using the `_` wildcard:\n+\n+```rust\n+match val.do_something() {\n+    Cat(a) => { /* ... */ }\n+    _      => { /* ... */ }\n+}\n+```\n+\n+<h2 id=\"numerics\">Numerics</h2>\n+\n+### Should I default to using `f32` or `f64`?\n+\n+The choice of which to use is dependent on the focus of the program.\n+\n+If you're interested in the greatest degree of precision with your floating point numbers, then `f64` is the preferred choice. If you're more interested in keeping the size of the value small, then `f32` is better.\n+\n+All things being equal, `f64` is likely a better default.\n+\n+### Why can't I compare floats?\n+\n+You can! Floats implement the `PartialOrd` trait, which means that `>`, `<`, `<=`, and `>=` are defined for them. But they do _not_ implement the `Ord` trait, because (thanks to `NaN`) there is no total ordering for floating point numbers. There [is a crate](https://crates.io/crates/ordered-float) that provides a total ordering on floats by wrapping them, which may be desirable depending on your use-case. You can still do all of the normal comparison operations you would expect, but you can't use the `cmp` function.\n+\n+### Why can't I use `f32` or `f64` as `HashMap` keys?\n+\n+In order to be used as a key in a `HashMap`, a type must implement the `Eq` and `Hash` traits. `f32` and `f64` implement `PartialEq`, but not `Eq`, because both types include `NaN` (stands for \"not a number\"). `NaN` values are [not equal to any float, and are not equal to each other](https://en.wikipedia.org/wiki/NaN). This means that `f32` and `f64` can't be used as keys in a HashMap.\n+\n+### How can I convert between numeric types?\n+\n+There are three ways: the `as` keyword, which does simple casting for primitive types, the `Into` and `From` traits, which are implemented for a number of type conversions (and which you can implement for your own types), and `transmute`, which is an unsafe function that tells the compiler to treat the bits of one type as those of another type.\n+\n+<h2 id=\"strings\">Strings</h2>\n+\n+### How can I convert a `String` or `Vec<T>` to a slice (`&str` and `&[T]`)?\n+\n+Using Deref coercions, `Strings` and `Vec`s will automatically coerce to their respective slices when passed by reference with `&` or `& mut`.\n+\n+### How can I convert from `&str` to `String` or the other way around?\n+\n+`String`s are automatically converted into `&str` when you take a reference of them. For example, the following code works:\n+\n+```rust\n+fn say_hello(name: &str) {\n+  println!(\"Hello {}!\", name);\n+}\n+\n+fn main() {\n+  let s = \"Jane Doe\".to_owned();\n+  say_hello(&s);\n+}\n+```\n+\n+The `to_owned()` is how you convert from a `&str` into a `String`. This is necessary because string literals in Rust are of type `&str`. In this particular example, making `s` into a `String` isn't actually necessary, and the code could be rewritten as:\n+\n+```rust\n+fn say_hello(name: &str) {\n+  println!(\"Hello {}!\", name);\n+}\n+\n+fn main() {\n+  let s = \"Jane Doe\";\n+  say_hello(s);\n+}\n+```\n+\n+### How do I split a `String` into lines?\n+\n+The [`lines()`](https://doc.rust-lang.org/stable/std/string/struct.String.html#method.lines) function for `String`s provides an iterator over the lines of a string, which can then be `collect()`ed into a container of lines like so:\n+\n+```rust\n+let s = \"This\\nis\\na\\ntest\";\n+let v: Vec<&str> = s.lines().collect();\n+```\n+\n+### What are the differences between the different string types?\n+\n+`String` is an owned string type, while `&str` is a string slice. For a more detailed explanation, [check out the Rust book](https://doc.rust-lang.org/stable/book/strings.html).\n+\n+### How do I do O(1) character access in a `String`?\n+\n+Rust strings are UTF-8 encoded. A single visual character in UTF-8 is not necessarily a single byte as it would be in an ASCII-encoded string. Each byte is called a \"code unit\" (In UTF-16, code units 2 bytes instead. In UTF-32 they are 4 bytes). \"Code points\" are composed of one or more code units, and combine in \"grapheme clusters\" which most closely approximate characters.\n+\n+Thus, even though you may index on bytes in a UTF-8 string, you can't access the `i`th code point or grapheme cluster in constant time. However, if you know at which byte that desired code point or grapheme cluster begins, then you _can_ access it in constant time. Functions including `str::find()` and regex matches return byte indices, facilitating this sort of access.\n+\n+### Why are strings UTF-8 by default?\n+\n+The `str` type is UTF-8 because we observe more text in the wild in this encoding – particularly in network transmissions, which are endian-agnostic – and we think it's best that the default treatment of I/O not involve having to recode codepoints in each direction.\n+\n+This does mean that locating a particular Unicode codepoint inside a string is an O(n) operation, although if the starting byte index is already known then they can be accessed in O(1) as expected. On the one hand, this is clearly undesirable; on the other hand, this problem is full of trade-offs and we'd like to point a few important qualifications:\n+\n+Scanning a `str` for ASCII-range codepoints can still be done safely byte-at-a-time. If you use `.as_bytes()`, pulling out a `u8` costs only `O(1)` and produces a value that can be cast and compared to an ASCII-range `char`. So if you're (say) line-breaking on `'\\n'`, byte-based treatment still works. UTF8 was well-designed this way.\n+\n+Most \"character oriented\" operations on text only work under very restricted language assumptions such as \"ASCII-range codepoints only\". Outside ASCII-range, you tend to have to use a complex (non-constant-time) algorithm for determining linguistic-unit (glyph, word, paragraph) boundaries anyway. We recommend using an \"honest\" linguistically-aware, Unicode-approved algorithm.\n+\n+The `char` type is UTF32. If you are sure you need to do a codepoint-at-a-time algorithm, it's trivial to write a `type wstr = [char]`, and unpack a `str` into it in a single pass, then work with the `wstr`. In other words: the fact that the language is not \"decoding to UTF32 by default\" shouldn't stop you from decoding (or re-encoding any other way) if you need to work with that encoding.\n+\n+For a more in-depth explanation of why UTF-8 is usually preferable over UTF-16 or UTF-32, read the [UTF-8 Everywhere manifesto](http://utf8everywhere.org/).\n+\n+<h2 id=\"collections\">Collections</h2>\n+\n+### Can I implement data structures like vectors and linked lists efficiently in Rust?\n+\n+If your reason for implementing these data structures is to use them for other programs, there's no need, as efficient implementations of these data structures are provided by the standard library.\n+\n+If, however, your reason is simply to learn, then you will likely need to dip into unsafe code. While these data structures _can_ be implemented entirely in safe Rust, the performance is likely to be worse than they would be with the use of unsafe code. The simple reason for this is that data structures like vectors and link lists rely on pointer and memory operations that are disallowed by safe Rust.\n+\n+For example, a doubly-linked list requires that there be two mutable references to each node, but this violates Rust's mutable reference aliasing rules. You can solve this using `Weak<T>`, but the performance will be poorer than you likely want.\n+\n+### How can I join a `Vec` (or an array) of strings into a single string?\n+\n+You can do this using the [`join()`](http://doc.rust-lang.org/std/slice/trait.SliceConcatExt.html#tymethod.join) and [`concat()`](http://doc.rust-lang.org/std/slice/trait.SliceConcatExt.html#tymethod.concat) iterator methods. These allow the concatenation of items being iterated over, with `join()` inserting a separator of your choosing.\n+\n+### How can I iterate over a `Vec<T>` without moving/consuming it?\n+\n+The easiest way is by using `Vec`'s `IntoIterator` implementation, like so:\n+\n+```rust\n+let v = vec![1,2,3,4,5];\n+for item in &v {\n+    print!(\"{} \", item);\n+}\n+println!(\"\\nLength: {}\", v.len());\n+```\n+\n+The way Rust `for` loops work, they actually call `into_iter()` (which is defined in the `IntoIterator` trait) for whatever you are trying to iterate over. `IntoIterator` is implemented for `&'a Vec<T>` and `&'a mut Vec<T>`, meaning you can iterate over a vector without consuming it just be using `&v` or `&mut v` (for some vector `v`).\n+\n+### Why do I need to type the array size in the array declaration?\n+\n+You don't necessarily have to. If you're declaring an array directly, the size is inferred based on the number of elements. But if you're declaring a function that takes an array, the compiler has to know how big that array will be.\n+\n+One thing to note is that currently Rust doesn't offer generics over arrays of different size. This is a planned feature, but isn't available currently. If you'd like to accept a contiguous container of a variable number of values, use a Vec or slice (depending on whether you need ownership).\n+\n+<h2 id=\"ownership\">Ownership</h2>\n+\n+### How can I implement a graph or other data structure that contains cycles?\n+\n+There are three major options:\n+\n+- You can implement it using `Weak` or `Rc` to allow shared ownership of nodes,\n+although this approach pays the cost of memory management.\n+- You can implement it using `unsafe` code using raw pointers. This will be\n+more efficient, but bypasses Rust's safety guarantees.\n+- Using vectors and indices into those vectors. There are [several](http://smallcultfollowing.com/babysteps/blog/2015/04/06/modeling-graphs-in-rust-using-vector-indices/) [available](http://featherweightmusings.blogspot.com/2015/04/graphs-in-rust.html) examples and explanations of this approach.\n+\n+### How can I define a struct that contains a pointer to one of its own fields?\n+\n+It's possible, but useless to do so. The struct becomes permanently borrowed by itself and therefore can't be moved. Here is some code illustrating this:\n+\n+```rust\n+use std::cell::Cell;\n+\n+#[derive(Debug)]\n+struct Unmovable<'a> {\n+    x: u32,\n+    y: Cell<Option<&'a u32>>,\n+}\n+\n+\n+fn main() {\n+    let test = Unmovable { x: 42, y: Cell::new(None) };\n+    test.y.set(Some(&test.x));\n+\n+    println!(\"{:?}\", test);\n+}\n+```\n+\n+### What does it mean to \"consume a value\"?\n+\n+\"Consuming a value\" means taking ownership of a value. When this is done, the value can't be used elsewhere.\n+\n+### What is the difference between consuming and moving/taking ownership?\n+\n+These are different terms for the same thing. In both cases, it means the value has been moved into a function, and moved out of the calling owner.\n+\n+### Why can I use integers after passing them to a function, but not structs?\n+\n+If a type implements the `Copy` trait, then it will be copied when passed to a function. All numeric types in Rust implement `Copy`, but struct types do not implement `Copy` by default, so they are moved instead. This means that the struct can no longer be used elsewhere, unless it is moved back out of the function via the return.\n+\n+### How do you deal with a \"Use of moved value\" error?\n+\n+This error means that the value you're trying to use has been moved to a new owner. The first thing to check is whether the move in question was necessary. If it moved into a function, it may be possible to rewrite the function to use a reference, rather than moving. It may also be possible to implement `Copy` (for implicit copying, rather than moving) or `Clone` (explicit copying) for the type in question. If none of these are possible, you may want to return ownership of the value when the function is done, thus eliminating this error.\n+\n+### What are the rules for using `self`, `&self`, or `&mut self` in a method declaration?\n+\n+- Use `self` when a function needs to consume the type\n+- Use `&self` when a function only needs a reference to the type\n+- Use `&mut self` when a function needs to mutate the type without consuming it\n+\n+### How can I understand the borrow checker?\n+\n+There is a certain desire to act as if the borrow checker is some mysterious zen master, doling out knocks on the head whenever its koans are misunderstood. The reality is a little different. In fact, the borrow checker is simply applying a couple simple rules, which can be found in the Rust book's [section on borrowing](https://doc.rust-lang.org/stable/book/references-and-borrowing.html#the-rules):\n+\n+> First, any borrow must last for a scope no greater than that of the owner. Second, you may have one or the other of these two kinds of borrows, but not both at the same time:\n+>\n+> - one or more references (&T) to a resource.\n+> - exactly one mutable reference (&mut T)\n+\n+Understanding these rules and [lifetimes](https://doc.rust-lang.org/stable/book/lifetimes.html) is all you need to do to understand the borrow checker.\n+\n+### How do deref coercions work?\n+\n+[Deref coercions](https://doc.rust-lang.org/book/deref-coercions.html) exist to make using Rust more ergonomic, and are implemented via the [`Deref`](https://doc.rust-lang.org/stable/std/ops/trait.Deref.html) trait, which looks like this:\n+\n+```rust\n+pub trait Deref {\n+    type Target: ?Sized;\n+    fn deref(&'a self) -> &'a Self::Target;\n+}\n+```\n+\n+A Deref implementation indicates that the implementing type may be converted into `Target` by a call to the `deref` function, which takes an immutable reference of a certain lifetime to the calling struct, and returns a reference of the same lifetime to the target.\n+\n+You can see a [full list of `Deref` implementations](https://doc.rust-lang.org/stable/std/ops/trait.Deref.html#implementors) for the standard library in the documentation.\n+\n+<h2 id=\"lifetimes\">Lifetimes</h2>\n+\n+### Why lifetimes?\n+\n+Lifetimes are Rust's answer to the question of memory safety. They allow Rust to ensure memory safety without mechanisms like garbage collection which carry hefty performance costs. They are based on a variety of academic work, which can be found in the [Rust book](https://doc.rust-lang.org/stable/book/academic-research.html#type-system).\n+\n+### Why is the lifetime syntax the way it is?\n+\n+The `'a` syntax comes from the ML family of programming languages, where `'a` is used to indicate a generic type parameter. For Rust, the syntax had to be something that was unambiguous, noticable, and fit nicely in a type declaration right alonside traits and references. Alternative syntaxes have been discussed, but this seems to work just fine.\n+\n+### When is `Rc` useful?\n+\n+This is covered in the [official documentation for `Rc`](https://doc.rust-lang.org/stable/std/rc/), Rust's non-atomically reference-counted pointer type. In short, `Rc` and its thread-safe cousin `Arc` are useful to express shared ownership of an immutable value, and have the system automatically deallocate the associated memory when no one has access to it.\n+\n+### How do I return a borrow to something I created from a function?\n+\n+You need to ensure that the borrowed item will outlive the function. This can be done in two ways: by binding the output lifetime to some input lifetime, or by declaring the output lifetime as static. The first option is significantly better than the second.\n+\n+Here is an example of each:\n+\n+```rust\n+// The first method\n+fn example1<'a>(s: &'a str) -> &'a str {\n+  // Do something...\n+}\n+\n+// The second method\n+fn example2<'a>(s: &'a str) -> &'static str {\n+  // Do something else...\n+}\n+```\n+\n+The problem with declaring the lifetime as `static` is that it's rarely what you actually mean, and is instead a way of escaping the lifetime system. References with a `static` lifetime outlive everything else, and this means that they can be used in ways that would otherwise be invalid with the first method.\n+\n+An alternative is to return an owning type like `String`. This eliminates the reference issues entirely, at the cost of possibly unnecessary allocations.\n+\n+There is also the `Cow` (\"copy on write\") type, which will only do the extra allocation if you attempt to mutate the contained value.\n+\n+### How do I return a closure from a function?\n+\n+To return a closure from a function, it must be a \"move closure\", meaning that the closure is declared with the `move` keyword. As [explained in the Rust book](https://doc.rust-lang.org/book/closures.html#move-closures), this gives the closure its own copy of the captured variables, independent of its parent stack frame. Otherwise, returning a closure would be unsafe, as it would allow access to variables that are no longer valid (put another way, it would allow reading potentially invalid memory). The closure must also be wrapped in a `Box`, so that it is allocated on the heap. Read more about this [in the book](https://doc.rust-lang.org/book/closures.html#returning-closures).\n+\n+### When are lifetimes required to be defined?\n+\n+Lifetimes can often be elided, as explained in the [\"Lifetime elision\" section](https://doc.rust-lang.org/book/lifetimes.html#lifetime-elision) of the Rust book. \"Elided lifetimes\" are those lifetimes which are implicit in any code containing references. They are automatically inserted by the compiler with the three following rules:\n+\n+- Each elided lifetime in a function’s arguments becomes a distinct lifetime parameter.\n+- If there is exactly one input lifetime, elided or not, that lifetime is assigned to all elided lifetimes in the return values of that function.\n+- If there are multiple input lifetimes, but one of them is &self or &mut self, the lifetime of self is assigned to all elided output lifetimes.\n+\n+If these rules would result in incorrect code elsewhere, then the Rust compiler will provide errors, and you will need to define the relevant lifetimes to correct that error.\n+\n+### How can Rust guarantee \"no null pointers\"?\n+\n+Data values in the language can only be constructed through a fixed set of initializer forms. Each of those forms requires that its inputs already be initialized. A liveness analysis ensures that local variables are initialized before use.\n+\n+<h2 id=\"generics\">Generics</h2>\n+\n+### What is \"monomorphisation\"?\n+\n+Monomorphisation is the process by which Rust generates specific instances of a generic function based on the types of the various calls to that function. This is used to provide static dispatch for generic functions. For functions using trait objects for generics, dynamic dispatch is used instead, with calls to the function going through a vtable to identify specific function calls for the provided type implementing the given trait.\n+\n+In C++ people would likely know this as \"template instantiation.\" But unlike C++, Rust's monomorphisation is an implementation detail, and not a language feature.\n+\n+### What's the difference between a function and a closure that doesn't capture any variables?\n+\n+Functions are a built-in primitive of the language, while closures are essentially syntactic sugar for one of three traits: `Fn`, `FnMut`, and `FnOnce`. When you make a closure, the Rust compiler automatically creates a struct implementing the appropriate trait of those three and containing the captured environment variables as members, and makes it so the the struct can be called as a function.\n+\n+The big difference between these traits is how they take the `self` parameter. `Fn` takes `&self`, `FnMut` takes `&mut self`, and `FnOnce` takes `self`.\n+\n+### What are higher-kinded types, why would I want them, and why doesn't Rust have them?\n+\n+Let's go through these one by one:\n+\n+First, higher-kinded types are types that take other types as parameters. Type constructors, like `Vec` are examples. What support for higher-kinded types means is that you can use these type constructors wherever you can use types, such as in a trait `impl`.\n+\n+Next, the lack of higher-kinded types makes expression of certain ideas far more tedious than it would otherwise be. For example, implementing a `Functor` trait (a math-y term for something which can be mapped over, obeying certain rules) without higher-kinded types is a pain. With higher-kinded types, it's as simple as (note: theoretical syntax, this does not actually work, and has no guarantee to be how higher-kinded types look when and if they're implemented):\n+\n+```rust\n+trait Functor {\n+    fn fmap<A, B, F: Fn(&A) -> B>(&self, f: F) -> Self<B>;\n+}\n+```\n+\n+The part that Rust currently can't do is that `Self` above is a type constructor parameterized by another type constructor. This is what support for higher-kinded types lets you express.\n+\n+Finally, Rust doesn't currently have them simply because they haven't been a priority. There is nothing inherent to the language that stops us from implementing higher-kinded types, it just hasn't been done yet. There is an open RFC for implementing them, but no real proposal exists yet. If one comes around, it will definitely be considered.\n+\n+(Credit to anasaziwochi for his [useful explanation](https://www.reddit.com/r/rust/comments/2av5tv/why_does_rust_not_have_a_functor_trait/ciz6iwm) of higher-kinded types in Rust.)\n+\n+### What do named type parameters in generic types mean?","path":"faq.md","position":416,"original_position":416,"commit_id":"57f72c57d290de64fb6b20913aeabd7b00fa4583","original_commit_id":"57f72c57d290de64fb6b20913aeabd7b00fa4583","user":{"login":"glaebhoerl","id":1216629,"avatar_url":"https://avatars.githubusercontent.com/u/1216629?v=3","gravatar_id":"","url":"https://api.github.com/users/glaebhoerl","html_url":"https://github.com/glaebhoerl","followers_url":"https://api.github.com/users/glaebhoerl/followers","following_url":"https://api.github.com/users/glaebhoerl/following{/other_user}","gists_url":"https://api.github.com/users/glaebhoerl/gists{/gist_id}","starred_url":"https://api.github.com/users/glaebhoerl/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/glaebhoerl/subscriptions","organizations_url":"https://api.github.com/users/glaebhoerl/orgs","repos_url":"https://api.github.com/users/glaebhoerl/repos","events_url":"https://api.github.com/users/glaebhoerl/events{/privacy}","received_events_url":"https://api.github.com/users/glaebhoerl/received_events","type":"User","site_admin":false},"body":"(FWIW I'm [wary](https://www.reddit.com/r/rust/comments/3k88i1/why_rust_trustworthy_concurrent_systems/cux2k5u?context=1) of the \"named type parameters\" analogy at all, although (or because) it does resemble that syntactically.)","created_at":"2015-11-20T09:41:23Z","updated_at":"2015-11-20T09:41:23Z","html_url":"https://github.com/rust-lang/rust-www/pull/202#discussion_r45450708","pull_request_url":"https://api.github.com/repos/rust-lang/rust-www/pulls/202","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust-www/pulls/comments/45450708"},"html":{"href":"https://github.com/rust-lang/rust-www/pull/202#discussion_r45450708"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rust-www/pulls/202"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/rust-www/pulls/202","id":48528101,"html_url":"https://github.com/rust-lang/rust-www/pull/202","diff_url":"https://github.com/rust-lang/rust-www/pull/202.diff","patch_url":"https://github.com/rust-lang/rust-www/pull/202.patch","issue_url":"https://api.github.com/repos/rust-lang/rust-www/issues/202","number":202,"state":"open","locked":false,"title":"Added Frequently Asked Questions Page","user":{"login":"AndrewBrinker","id":1223605,"avatar_url":"https://avatars.githubusercontent.com/u/1223605?v=3","gravatar_id":"","url":"https://api.github.com/users/AndrewBrinker","html_url":"https://github.com/AndrewBrinker","followers_url":"https://api.github.com/users/AndrewBrinker/followers","following_url":"https://api.github.com/users/AndrewBrinker/following{/other_user}","gists_url":"https://api.github.com/users/AndrewBrinker/gists{/gist_id}","starred_url":"https://api.github.com/users/AndrewBrinker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndrewBrinker/subscriptions","organizations_url":"https://api.github.com/users/AndrewBrinker/orgs","repos_url":"https://api.github.com/users/AndrewBrinker/repos","events_url":"https://api.github.com/users/AndrewBrinker/events{/privacy}","received_events_url":"https://api.github.com/users/AndrewBrinker/received_events","type":"User","site_admin":false},"body":"This commit is the first draft of a new Frequently Asked Questions (FAQ) page for the Rust website. It provides answers for all but 4 of the 130 questions asked by the community, which can be found on the tracking issue #181.\r\n\r\nThe answers could use some review, as could the design and organization. I've tried to provide clear, concise answers that more often than not point people to the relevant portion of the Rust book.\r\n\r\nThoughts? Changes?","created_at":"2015-10-22T23:54:10Z","updated_at":"2015-11-20T09:41:23Z","closed_at":null,"merged_at":null,"merge_commit_sha":"bb351d8fa5df405d217f155f1b94e40b05ebe22b","assignee":{"login":"brson","id":147214,"avatar_url":"https://avatars.githubusercontent.com/u/147214?v=3","gravatar_id":"","url":"https://api.github.com/users/brson","html_url":"https://github.com/brson","followers_url":"https://api.github.com/users/brson/followers","following_url":"https://api.github.com/users/brson/following{/other_user}","gists_url":"https://api.github.com/users/brson/gists{/gist_id}","starred_url":"https://api.github.com/users/brson/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/brson/subscriptions","organizations_url":"https://api.github.com/users/brson/orgs","repos_url":"https://api.github.com/users/brson/repos","events_url":"https://api.github.com/users/brson/events{/privacy}","received_events_url":"https://api.github.com/users/brson/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/rust-www/pulls/202/commits","review_comments_url":"https://api.github.com/repos/rust-lang/rust-www/pulls/202/comments","review_comment_url":"https://api.github.com/repos/rust-lang/rust-www/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/rust-www/issues/202/comments","statuses_url":"https://api.github.com/repos/rust-lang/rust-www/statuses/57f72c57d290de64fb6b20913aeabd7b00fa4583","head":{"label":"AndrewBrinker:faq","ref":"faq","sha":"57f72c57d290de64fb6b20913aeabd7b00fa4583","user":{"login":"AndrewBrinker","id":1223605,"avatar_url":"https://avatars.githubusercontent.com/u/1223605?v=3","gravatar_id":"","url":"https://api.github.com/users/AndrewBrinker","html_url":"https://github.com/AndrewBrinker","followers_url":"https://api.github.com/users/AndrewBrinker/followers","following_url":"https://api.github.com/users/AndrewBrinker/following{/other_user}","gists_url":"https://api.github.com/users/AndrewBrinker/gists{/gist_id}","starred_url":"https://api.github.com/users/AndrewBrinker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndrewBrinker/subscriptions","organizations_url":"https://api.github.com/users/AndrewBrinker/orgs","repos_url":"https://api.github.com/users/AndrewBrinker/repos","events_url":"https://api.github.com/users/AndrewBrinker/events{/privacy}","received_events_url":"https://api.github.com/users/AndrewBrinker/received_events","type":"User","site_admin":false},"repo":{"id":41641399,"name":"rust-www","full_name":"AndrewBrinker/rust-www","owner":{"login":"AndrewBrinker","id":1223605,"avatar_url":"https://avatars.githubusercontent.com/u/1223605?v=3","gravatar_id":"","url":"https://api.github.com/users/AndrewBrinker","html_url":"https://github.com/AndrewBrinker","followers_url":"https://api.github.com/users/AndrewBrinker/followers","following_url":"https://api.github.com/users/AndrewBrinker/following{/other_user}","gists_url":"https://api.github.com/users/AndrewBrinker/gists{/gist_id}","starred_url":"https://api.github.com/users/AndrewBrinker/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AndrewBrinker/subscriptions","organizations_url":"https://api.github.com/users/AndrewBrinker/orgs","repos_url":"https://api.github.com/users/AndrewBrinker/repos","events_url":"https://api.github.com/users/AndrewBrinker/events{/privacy}","received_events_url":"https://api.github.com/users/AndrewBrinker/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/AndrewBrinker/rust-www","description":"The Rust website (www.rust-lang.org)","fork":true,"url":"https://api.github.com/repos/AndrewBrinker/rust-www","forks_url":"https://api.github.com/repos/AndrewBrinker/rust-www/forks","keys_url":"https://api.github.com/repos/AndrewBrinker/rust-www/keys{/key_id}","collaborators_url":"https://api.github.com/repos/AndrewBrinker/rust-www/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/AndrewBrinker/rust-www/teams","hooks_url":"https://api.github.com/repos/AndrewBrinker/rust-www/hooks","issue_events_url":"https://api.github.com/repos/AndrewBrinker/rust-www/issues/events{/number}","events_url":"https://api.github.com/repos/AndrewBrinker/rust-www/events","assignees_url":"https://api.github.com/repos/AndrewBrinker/rust-www/assignees{/user}","branches_url":"https://api.github.com/repos/AndrewBrinker/rust-www/branches{/branch}","tags_url":"https://api.github.com/repos/AndrewBrinker/rust-www/tags","blobs_url":"https://api.github.com/repos/AndrewBrinker/rust-www/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/AndrewBrinker/rust-www/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/AndrewBrinker/rust-www/git/refs{/sha}","trees_url":"https://api.github.com/repos/AndrewBrinker/rust-www/git/trees{/sha}","statuses_url":"https://api.github.com/repos/AndrewBrinker/rust-www/statuses/{sha}","languages_url":"https://api.github.com/repos/AndrewBrinker/rust-www/languages","stargazers_url":"https://api.github.com/repos/AndrewBrinker/rust-www/stargazers","contributors_url":"https://api.github.com/repos/AndrewBrinker/rust-www/contributors","subscribers_url":"https://api.github.com/repos/AndrewBrinker/rust-www/subscribers","subscription_url":"https://api.github.com/repos/AndrewBrinker/rust-www/subscription","commits_url":"https://api.github.com/repos/AndrewBrinker/rust-www/commits{/sha}","git_commits_url":"https://api.github.com/repos/AndrewBrinker/rust-www/git/commits{/sha}","comments_url":"https://api.github.com/repos/AndrewBrinker/rust-www/comments{/number}","issue_comment_url":"https://api.github.com/repos/AndrewBrinker/rust-www/issues/comments{/number}","contents_url":"https://api.github.com/repos/AndrewBrinker/rust-www/contents/{+path}","compare_url":"https://api.github.com/repos/AndrewBrinker/rust-www/compare/{base}...{head}","merges_url":"https://api.github.com/repos/AndrewBrinker/rust-www/merges","archive_url":"https://api.github.com/repos/AndrewBrinker/rust-www/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/AndrewBrinker/rust-www/downloads","issues_url":"https://api.github.com/repos/AndrewBrinker/rust-www/issues{/number}","pulls_url":"https://api.github.com/repos/AndrewBrinker/rust-www/pulls{/number}","milestones_url":"https://api.github.com/repos/AndrewBrinker/rust-www/milestones{/number}","notifications_url":"https://api.github.com/repos/AndrewBrinker/rust-www/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/AndrewBrinker/rust-www/labels{/name}","releases_url":"https://api.github.com/repos/AndrewBrinker/rust-www/releases{/id}","created_at":"2015-08-30T20:05:31Z","updated_at":"2015-08-30T20:05:31Z","pushed_at":"2015-11-18T23:08:07Z","git_url":"git://github.com/AndrewBrinker/rust-www.git","ssh_url":"git@github.com:AndrewBrinker/rust-www.git","clone_url":"https://github.com/AndrewBrinker/rust-www.git","svn_url":"https://github.com/AndrewBrinker/rust-www","homepage":"","size":3442,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"9f51aec7640ea7731779bf511e71ed9a9997259f","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":2302682,"name":"rust-www","full_name":"rust-lang/rust-www","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/rust-www","description":"The Rust website (www.rust-lang.org)","fork":false,"url":"https://api.github.com/repos/rust-lang/rust-www","forks_url":"https://api.github.com/repos/rust-lang/rust-www/forks","keys_url":"https://api.github.com/repos/rust-lang/rust-www/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/rust-www/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/rust-www/teams","hooks_url":"https://api.github.com/repos/rust-lang/rust-www/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/rust-www/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/rust-www/events","assignees_url":"https://api.github.com/repos/rust-lang/rust-www/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/rust-www/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/rust-www/tags","blobs_url":"https://api.github.com/repos/rust-lang/rust-www/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/rust-www/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/rust-www/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/rust-www/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/rust-www/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/rust-www/languages","stargazers_url":"https://api.github.com/repos/rust-lang/rust-www/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/rust-www/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/rust-www/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/rust-www/subscription","commits_url":"https://api.github.com/repos/rust-lang/rust-www/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/rust-www/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/rust-www/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/rust-www/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/rust-www/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/rust-www/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/rust-www/merges","archive_url":"https://api.github.com/repos/rust-lang/rust-www/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/rust-www/downloads","issues_url":"https://api.github.com/repos/rust-lang/rust-www/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/rust-www/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/rust-www/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/rust-www/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/rust-www/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/rust-www/releases{/id}","created_at":"2011-08-31T16:42:32Z","updated_at":"2015-11-20T00:56:20Z","pushed_at":"2015-11-18T23:08:08Z","git_url":"git://github.com/rust-lang/rust-www.git","ssh_url":"git@github.com:rust-lang/rust-www.git","clone_url":"https://github.com/rust-lang/rust-www.git","svn_url":"https://github.com/rust-lang/rust-www","homepage":"","size":4497,"stargazers_count":26,"watchers_count":26,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":68,"mirror_url":null,"open_issues_count":31,"forks":68,"open_issues":31,"watchers":26,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust-www/pulls/202"},"html":{"href":"https://github.com/rust-lang/rust-www/pull/202"},"issue":{"href":"https://api.github.com/repos/rust-lang/rust-www/issues/202"},"comments":{"href":"https://api.github.com/repos/rust-lang/rust-www/issues/202/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/rust-www/pulls/202/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/rust-www/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/rust-www/pulls/202/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/rust-www/statuses/57f72c57d290de64fb6b20913aeabd7b00fa4583"}}}}