]> git.lizzy.rs Git - rust.git/blob - src/doc/complement-lang-faq.md
Auto merge of #29651 - tshepang:misc, r=steveklabnik
[rust.git] / src / doc / complement-lang-faq.md
1 % The Rust Language FAQ
2
3 ## Are there any big programs written in it yet? I want to read big samples.
4
5 There aren't many large programs yet. The Rust [compiler][rustc], 60,000+ lines at the time of writing, is written in Rust. As the oldest body of Rust code it has gone through many iterations of the language, and some parts are nicer to look at than others. It may not be the best code to learn from, but [borrowck] and [resolve] were written recently.
6
7 [rustc]: https://github.com/rust-lang/rust/tree/master/src/librustc
8 [resolve]: https://github.com/rust-lang/rust/tree/master/src/librustc_resolve
9 [borrowck]: https://github.com/rust-lang/rust/tree/master/src/librustc_borrowck/borrowck
10
11 A research browser engine called [Servo][servo], currently 30,000+ lines across more than a dozen crates, will be exercising a lot of Rust's distinctive type-system and concurrency features, and integrating many native libraries.
12
13 [servo]: https://github.com/servo/servo
14
15 Some examples that demonstrate different aspects of the language:
16
17 * [sprocketnes], an NES emulator with no GC, using modern Rust conventions
18 * The language's general-purpose [hash] function, SipHash-2-4. Bit twiddling, OO, macros
19 * The standard library's [HashMap], a sendable hash map in an OO style
20 * The standard library's [json] module. Enums and pattern matching
21
22 [sprocketnes]: https://github.com/pcwalton/sprocketnes
23 [hash]: https://github.com/rust-lang/rust/tree/master/src/libcore/hash
24 [HashMap]: https://github.com/rust-lang/rust/tree/master/src/libstd/collections/hash
25 [json]: https://github.com/rust-lang/rust/blob/master/src/libserialize/json.rs
26
27 You may also be interested in browsing [trending Rust repositories][github-rust] on GitHub.
28
29 [github-rust]: https://github.com/trending?l=rust
30
31 ## Is anyone using Rust in production?
32
33 Yes. For example (incomplete):
34
35 * [OpenDNS](http://labs.opendns.com/2013/10/04/zeromq-helping-us-block-malicious-domains/)
36 * [Skylight](http://skylight.io)
37 * [wit.ai](https://github.com/wit-ai/witd)
38 * [Codius](https://codius.org/blog/codius-rust/)
39 * [MaidSafe](http://maidsafe.net/)
40 * [Terminal.com](https://terminal.com)
41
42 ## Does it run on Windows?
43
44 Yes. All development happens in lockstep on all 3 target platforms (using MinGW, not Cygwin).
45
46 ## Is it OO? How do I do this thing I normally do in an OO language?
47
48 It is multi-paradigm. Not everything is shoe-horned into a single abstraction. Many things you can do in OO languages you can do in Rust, but not everything, and not always using the same abstraction you're accustomed to.
49
50 ## How do you get away with "no null pointers"?
51
52 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.
53
54 ## What is the relationship between a module and a crate?
55
56 * A crate is a top-level compilation unit that corresponds to a single loadable object.
57 * A module is a (possibly nested) unit of name-management inside a crate.
58 * A crate contains an implicit, un-named top-level module.
59 * Recursive definitions can span modules, but not crates.
60 * Crates do not have global names, only a set of non-unique metadata tags.
61 * There is no global inter-crate namespace; all name management occurs within a crate.
62  * Using another crate binds the root of _its_ namespace into the user's namespace.
63
64 ## Why is panic unwinding non-recoverable within a thread? Why not try to "catch exceptions"?
65
66 In short, because too few guarantees could be made about the dynamic environment of the catch block, as well as invariants holding in the unwound heap, to be able to safely resume; we believe that other methods of signalling and logging errors are more appropriate, with threads playing the role of a "hard" isolation boundary between separate heaps.
67
68 Rust provides, instead, three predictable and well-defined options for handling any combination of the three main categories of "catch" logic:
69
70 * Failure _logging_ is done by the integrated logging subsystem.
71 * _Recovery_ after a panic is done by trapping a thread panic from _outside_
72   the thread, where other threads are known to be unaffected.
73 * _Cleanup_ of resources is done by RAII-style objects with destructors.
74
75 Cleanup through RAII-style destructors is more likely to work than in catch blocks anyways, since it will be better tested (part of the non-error control paths, so executed all the time).
76
77 ## Why aren't modules type-parametric?
78
79 We want to maintain the option to parameterize at runtime. We may eventually change this limitation, but initially this is how type parameters were implemented.
80
81 ## Why aren't values type-parametric? Why only items?
82
83 Doing so would make type inference much more complex, and require the implementation strategy of runtime parameterization.
84
85 ## Why are enumerations nominal and closed?
86
87 We don't know if there's an obvious, easy, efficient, stock-textbook way of supporting open or structural disjoint unions. We prefer to stick to language features that have an obvious and well-explored semantics.
88
89 ## Why aren't channels synchronous?
90
91 There's a lot of debate on this topic; it's easy to find a proponent of default-sync or default-async communication, and there are good reasons for either. Our choice rests on the following arguments:
92
93 * Part of the point of isolating threads is to decouple threads from one another, such that assumptions in one thread do not cause undue constraints (or bugs, if violated!) in another. Temporal coupling is as real as any other kind; async-by-default relaxes the default case to only _causal_ coupling.
94 * Default-async supports buffering and batching communication, reducing the frequency and severity of thread-switching and inter-thread / inter-domain synchronization.
95 * Default-async with transmittable channels is the lowest-level building block on which more-complex synchronization topologies and strategies can be built; it is not clear to us that the majority of cases fit the 2-party full-synchronization pattern rather than some more complex multi-party or multi-stage scenario. We did not want to force all programs to pay for wiring the former assumption into all communications.
96
97 ## Why are channels half-duplex (one-way)?
98
99 Similar to the reasoning about default-sync: it wires fewer assumptions into the implementation, that would have to be paid by all use-cases even if they actually require a more complex communication topology.
100
101 ## Why are strings UTF-8 by default? Why not UCS2 or UCS4?
102
103 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.
104
105 This does mean that indexed access to a Unicode codepoint inside a `str` value is an O(n) operation. 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:
106
107 * Scanning a `str` for ASCII-range codepoints can still be done safely octet-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'`, octet-based treatment still works. UTF8 was well-designed this way.
108 * Most "character oriented" operations on text only work under very restricted language assumptions sets 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 anyways. We recommend using an "honest" linguistically-aware, Unicode-approved algorithm.
109 * The `char` type is UCS4. If you honestly 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 UCS4 by default" shouldn't stop you from decoding (or re-encoding any other way) if you need to work with that encoding.
110
111 ## Why are `str`s, slices, arrays etc. built-in types rather than (say) special kinds of trait/impl?
112
113 In each case there is one or more operator, literal constructor, overloaded use or integration with a built-in control structure that makes us think it would be awkward to phrase the type in terms of more-general type constructors. Same as, say, with numbers! But this is partly an aesthetic call, and we'd be willing to look at a worked-out proposal for eliminating or rephrasing these special cases.
114
115 ## Can Rust code call C code?
116
117 Yes. Calling C code from Rust is simple and exactly as efficient as calling C code from C.
118
119 ## Can C code call Rust code?
120
121 Yes. The Rust code has to be exposed via an `extern` declaration, which makes it C-ABI compatible. Such a function can be passed to C code as a function pointer or, if given the `#[no_mangle]` attribute to disable symbol mangling, can be called directly from C code.
122
123 ## Why aren't function signatures inferred? Why only local variables?
124
125 * Mechanically, it simplifies the inference algorithm; inference only requires looking at one function at a time.
126 * The same simplification goes double for human readers. A reader does not need 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.
127
128 ## Why does a type parameter need explicit trait bounds to invoke methods on it, when C++ templates do not?
129
130 * Requiring explicit bounds means that the compiler can type-check the code at the point where the type-parametric item is *defined*, rather than delaying to when its type parameters are instantiated.  You know that *any* set of type parameters fulfilling the bounds listed in the API will compile. It's an enforced minimal level of documentation, and results in very clean error messages.
131
132 * Scoping of methods is also a problem.  C++ needs [Koenig (argument dependent) lookup](http://en.wikipedia.org/wiki/Argument-dependent_name_lookup), which comes with its own host of problems. Explicit bounds avoid this issue: traits are explicitly imported and then used as bounds on type parameters, so there is a clear mapping from the method to its implementation (via the trait and the instantiated type).
133
134   * Related to the above point: since a parameter explicitly names its trait bounds, a single type is able to implement traits whose sets of method names overlap, cleanly and unambiguously.
135
136 * There is further discussion on [this thread on the Rust mailing list](https://mail.mozilla.org/pipermail/rust-dev/2013-September/005603.html).
137
138 ## Will Rust implement automatic semicolon insertion, like in Go?
139
140 For simplicity, we do not plan to do so. Implementing automatic semicolon insertion for Rust would be tricky because the absence of a trailing semicolon means "return a value".
141
142 ## How do I get my program to display the output of logging macros?
143
144 **Short Answer**: Set the `RUST_LOG` environment variable to the name of your source file, sans extension.
145
146 ```sh
147 rustc hello.rs
148 export RUST_LOG=hello
149 ./hello
150 ```
151
152 **Long Answer**: `RUST_LOG` takes a 'logging spec' that consists of a
153 comma-separated list of paths, where a path consists of the crate name and
154 sequence of module names, each separated by double-colons. For standalone `.rs`
155 files, the crate is implicitly named after the source file, so in the above
156 example we were setting `RUST_LOG` to the name of the hello crate. Multiple paths
157 can be combined to control the exact logging you want to see. For example, when
158 debugging linking in the compiler, you might set the following:
159
160 ```sh
161 RUST_LOG=rustc_metadata::creader,rustc::util::filesearch,rustc::back::rpath
162 ```
163
164 For a full description, see [the logging crate][1].
165
166 ## How fast is Rust?
167
168 As always, this question is difficult to answer. There's still a lot of work to
169 do on speed, and depending on what you're benchmarking, Rust has variable
170 performance.
171
172 That said, it is an explicit goal of Rust to be as fast as C++ for most things.
173 Language decisions are made with performance in mind, and we want Rust to be as
174 fast as possible. Given that Rust is built on top of LLVM, any performance
175 improvements in it also help Rust become faster.
176
177 [1]:log/index.html