Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAdded Frequently Asked Questions Page #202
Conversation
alilleybrinker
added some commits
Sep 1, 2015
rust-highfive
assigned
brson
Oct 22, 2015
This comment has been minimized.
This comment has been minimized.
rust-highfive
commented
Oct 22, 2015
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @brson (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
This comment has been minimized.
This comment has been minimized.
|
\o/ Woah! So much web site. Thanks. |
This comment has been minimized.
This comment has been minimized.
Diggsey
commented
Oct 23, 2015
|
Some thoughts while reading through it:
|
This comment has been minimized.
This comment has been minimized.
|
Wonderful! This is exactly the input I'm looking for:
Ah, mistake during editing. Fixing now.
Good to know. I've updated my answer accordingly.
Do you know which crate?
Good point. Updated.
Do you have some example code that does this? I'd like to provide an example if I can.
You're right. Fixed.
I agree. I've clarified the language.
Good point. I've updated the answer.
Yup. I copied this answer verbatim from a much older FAQ on the Rust website. It may have been true at one point (I don't know), but you're right that it isn't now.
I've added an answer addressing this.
Good idea. Added.
Yup. Added.
Added, along with a small example of such a function. |
This comment has been minimized.
This comment has been minimized.
|
Before anything gets merged I do want to note that four questions remain unanswered and commented out in the source code. This does not necessarily need to block merging, but if someone can answer any of these questions before we merge that would be much appreciated. If not, then afterward works. |
This comment has been minimized.
This comment has been minimized.
Diggsey
commented
Oct 23, 2015
https://crates.io/crates/ordered-float
|
This comment has been minimized.
This comment has been minimized.
|
Okay, I've updated it with that information. |
This comment has been minimized.
This comment has been minimized.
|
I think this is ready to merge, unless there are concerns about the specialized styling for the page. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
|
GuillaumeGomez
reviewed
Nov 5, 2015
|
|
||
| As always, this question is difficult to answer. There's still a lot of work to do on speed, and depending on what you're benchmarking, Rust has variable performance. | ||
|
|
||
| That said, it is an explicit goal of Rust to be as fast as C++ for most things. Language decisions are made with performance in mind, and we want Rust to be as fast as possible. Given that Rust is built on top of LLVM, any performance improvements in it also help Rust become faster. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
tshepang
Nov 18, 2015
Contributor
@brson why is it important to mention "idiomatic"? Is it because on some benchmarks, people use non-idiomatic C++ to win?
This comment has been minimized.
This comment has been minimized.
brson
Nov 18, 2015
Contributor
That's right. Pedants can easily write horrible C++ code that beats safe
Rust as a counter-argument to performance claims, so we typically qualify
them with 'idiomatic C++'.
On the other hand though, in many cases safe Rust can trounce even
horribly-unsafe C++, and its our prerogative to make strong claims to sell
Rust (I don't mind ruffling feathers a bit). I'll think about it.
On Wed, Nov 18, 2015 at 4:53 AM, Tshepang Lekhonkhobe <
notifications@github.com> wrote:
In faq.md
#202 (comment):
<li><a href="#cross-platform">Cross-Platform</a></li><li><a href="#design-patterns">Design Patterns</a></li><li><a href="#macros">Macros</a></li><li><a href="#other-languages">Other Languages</a></li><li><a href="#licensing">Licensing</a></li><li><a href="#naming">Naming</a></li>+
+
+## Performance
+
+#### How fast is Rust?
+
+As always, this question is difficult to answer. There's still a lot of work to do on speed, and depending on what you're benchmarking, Rust has variable performance.
+
+That said, it is an explicit goal of Rust to be as fast as C++ for most things. Language decisions are made with performance in mind, and we want Rust to be as fast as possible. Given that Rust is built on top of LLVM, any performance improvements in it also help Rust become faster.@brson https://github.com/brson why is it important to mention
"idiomatic"? Is it because on some benchmarks, people use non-idiomatic C++
to win?—
Reply to this email directly or view it on GitHub
https://github.com/rust-lang/rust-www/pull/202/files#r45194937.
brson
reviewed
Nov 5, 2015
| Licensed under the Apache License, Version 2.0 or the MIT license, at your option.<br> | ||
| This file may not be copied, modified, or distributed except according to those terms.</p> | ||
| </div> | ||
| </div> |
This comment has been minimized.
This comment has been minimized.
brson
Nov 5, 2015
Contributor
Can we not use a custom layout for this? Is the only customization here the copyright disclaimer? If so I feel ok, for now at least, assuming that the copyright declaration on the www repo itself is sufficient.
This is an interesting choice though, and we should consider whether something like it should be done for the entire site.
This comment has been minimized.
This comment has been minimized.
alilleybrinker
Nov 5, 2015
Author
Contributor
The custom layout is actually for an interesting reason. Markdown doesn't allow having Markdown embedded in HTML tags, and the <div class="faq"> is used for some custom styling (which may be undesirable, and definitely needs a bit of fine-tuning for mobile). I prefer to write in Markdown, although the file could of course be transitioned to entirely HTML if the extra layout is undesirable.
This comment has been minimized.
This comment has been minimized.
|
cc me. We need to advertise this PR very widely, get the whole community's eyes on it. |
This comment has been minimized.
This comment has been minimized.
|
Could this be formatted so each question has an anchor, and I can link to a specific section? I imagine needing to link particular questions to new users quite frequently, and it'd be better to include the section in the URL than to tell them "click here and scrolll" |
brson
reviewed
Nov 5, 2015
| # Frequently Asked Questions | ||
|
|
||
| This document exists to answer some 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. Rather, it exists as a reference to answer oft-repeated questions people in the Rust community encounter, and to clarify some of the design and history of the language. | ||
|
|
This comment has been minimized.
This comment has been minimized.
brson
Nov 5, 2015
Contributor
This para explains why this page 'exists' twice (first sentence and third). It's not bad but feels like it could be tightened up. No suggestions offhand.
brson
reviewed
Nov 5, 2015
|
|
||
| #### How fast is Rust? | ||
|
|
||
| As always, this question is difficult to answer. There's still a lot of work to do on speed, and depending on what you're benchmarking, Rust has variable performance. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
brson
Nov 5, 2015
Contributor
Before demurring about performance we might want to say 'Rust is fast'. It is. We can rightfully make some strong claims.
Could even link to sources to back up those claims.
This comment has been minimized.
This comment has been minimized.
alilleybrinker
Nov 5, 2015
Author
Contributor
Absolutely. Are there particular sources or benchmarks which would be preferred targets for links? I know some people have expressed problems with the benchmarks like the Benchmarks Game in the past.
nikomatsakis
reviewed
Nov 5, 2015
|
|
||
| Rust has a [very small and limited runtime]() providing a heap, unwinding and backtrace support, 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. | ||
|
|
||
| ## Concurrency |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Nov 5, 2015
Contributor
It feels like there are more concurrency questions that should be listed here. For example, I frequently hear people express surprise that Rust supports mutexes, since early versions of the language supported only message passing. Perhaps a question explaining this would make sense.
This comment has been minimized.
This comment has been minimized.
alilleybrinker
Nov 5, 2015
Author
Contributor
I am happy to add some more concurrency questions and answers. The included answers right now are a combination of topics covered in previous FAQs (updated, of course) and questions suggested after soliciting input from the community. It may simply be that a second round of question seeking may be in order, with particular focus on concurrency and any other sections currently under-covered by the FAQ.
nagisa
reviewed
Nov 5, 2015
brson
reviewed
Nov 5, 2015
| #### Is Rust garbage collected? | ||
|
|
||
| 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. Additionally, garbage collection is frequently a source of non-deterministic behavior. Rust provides the tools to make using a GC possible and even pleasant, but it should not be a requirement for implementing the language. | ||
|
|
This comment has been minimized.
This comment has been minimized.
brson
Nov 5, 2015
Contributor
If we're going to claim that GC is possible seems like we should link to something to back that up.
"should not be a requirement" - "should" feels too weak since it's just a fact there's no GC in Rust. Maybe 'Rust provides the tools..., but does not include a garbage collector in the language or its standard libraries'.
sfackler
reviewed
Nov 5, 2015
|
|
||
| #### 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? | ||
|
|
||
| A single line block may not always be a single line block. Forgetting to add in the brackets when you add a line to an `if` block can be the cause of some serious programming errors (just look at Apple's [“goto fail”](https://gotofail.com/) bug). In the interest of safety, Rust makes you use the blocks every time. |
This comment has been minimized.
This comment has been minimized.
sfackler
Nov 5, 2015
Member
I would not say that goto fail is the primary motivation here. A bigger point is that parsing becomes ambiguous or at least confusing when both the parens around the condition and the braces around the block are omitted:
if foo == baz && bar.thingy() < 10 buz = 100;
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
vadimcn
Nov 6, 2015
Contributor
Exactly. The "safety" justification is dubious anyways: if someone was paying so little attention to pasted code, he could have pasted the second goto after the closing bracket to the same effect. If anything this bug argues in favor of indentation-sensitive syntax.
Ability to drop brackets on conditionals in control structures is a much better justification, IMO.
mbrubeck
reviewed
Nov 5, 2015
|
|
||
| #### How can I join a `Vec` (or an array) of strings into a single string? | ||
|
|
||
| Whether the strings are owned strings (`String`) or string slices (`&str`), you can accomplish this with a fold, like so: |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
reviewed
Nov 5, 2015
| - You can implement it using `Weak` or `Rc` to allow shared ownership of nodes, | ||
| although this approach pays the cost of memory management. | ||
| - You can implement it using `unsafe` code using raw pointers. This will be | ||
| more efficient, but bypasses Rust's safety guarantees. |
This comment has been minimized.
This comment has been minimized.
nikomatsakis
Nov 5, 2015
Contributor
Indices and vectors work well too. Here are two blog posts covering this topic:
sfackler
reviewed
Nov 5, 2015
|
|
||
| #### How do I read file input efficiently? | ||
|
|
||
| The [`File` type](https://doc.rust-lang.org/stable/std/fs/struct.File.html) implements the `Read` trait, which has a variety of functions for reading and writing data, including `read()`, `read_to_end()`, `bytes()`, `chars()`, and `take()`. Each of these functions reads in a certain amount of input from a given file. `read()` reads input until the provided buffer is full. `read_to_end()` reads the entire buffer into a vector, allocating as much space as is needed. `bytes()` and `chars()` allow you to iterate over the bytes and characters of the file, respectively. Finally, `take()` allows you to read up to an arbitrary number of bytes from the file. Collectively, these should allow you to efficiently read in any data you need. |
This comment has been minimized.
This comment has been minimized.
sfackler
Nov 5, 2015
Member
read does not read until the buffer is full, but rather as much as the underlying system is willing to provide in a single call. read_exact does, which is unstable right now but will probably stabilize for 1.6.
This comment has been minimized.
This comment has been minimized.
brson
reviewed
Nov 5, 2015
| #### Why is my program compiled with `cargo build` slow? | ||
|
|
||
| Did you compile with the `--release` flag? The Rust language uses a lot of optimizations in release mode that make the language competitive with C and C++, but you need to explicitly ask for them, as they also result in longer compilation times that may be undesirable during development. | ||
|
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Toby-S
Nov 6, 2015
The heading here reads a bit unnaturally for me... perhaps "Why are programs compiled with cargo build slow?" or the more general "Why is my program slow?"?
This comment has been minimized.
This comment has been minimized.
|
Obviously things aren't quite done yet. I have some more direct feedback to go over, and I need to address @brson's previous points. But I just want to say that this has been a really great process, and I can't wait to get this thing polished and merged. Thanks for all the help and feedback, everyone. Oh, and no problem @steveklabnik. It's been my pleasure. |
pnkfelix
reviewed
Jan 8, 2016
|
|
||
| Rust `for` loops call `into_iter()` (defined on the `IntoIterator` trait) for whatever they're iterating over. Anything implementing the `IntoIterator` trait may be looped over with a `for` loop. `IntoIterator` is implemented for `&Vec` and `&mut Vec`, causing the iterator from `into_iter()` to borrow the contents of the collection, rather than moving/consuming them. The same is true for other standard collections as well. | ||
|
|
||
| If a moving/consuming iterator is desired, write the `for` loop without `&` or `&mut` on the collection. |
This comment has been minimized.
This comment has been minimized.
pnkfelix
Jan 8, 2016
Member
You mean "without & or &mut in the iteration", right?
(Not "collection", that is)
This comment has been minimized.
This comment has been minimized.
alilleybrinker
added some commits
Jan 8, 2016
This comment has been minimized.
This comment has been minimized.
|
(great job incorporating feedback @AndrewBrinker ; I was especially impressed by the places where I said "oh man this is wrong but I don't know how to fix it" (and I threw up my hands) and you subsequently found a solution, quite quickly!) |
This comment has been minimized.
This comment has been minimized.
|
Thanks @pnkfelix! I'm glad you like what I've done. It wouldn't have been possible without all the great feedback from everyone. The current version is much much better than my first draft. It'll be interesting to see how the document evolves over time, and to see what sort of feedback we get once it's up on the site. @brson, I know you have some style changes you'd like made, and that your plan is to wrap this up yourself (I'm guessing squash down all the commits in this PR). Should I incorporate the style changes here, or is that something you'd like to do? Also, I am still working on your big list of thoughts. The deref coercion and higher-kinded types answers have been updated, but the rest still need to be addressed properly. |
This comment has been minimized.
This comment has been minimized.
|
Also, I started adding a bunch of missing links to the API docs at @steveklabnik's suggestion, but I'm not finished yet. |
solson
reviewed
Jan 8, 2016
|
|
||
| ### When should I use an implicit return? | ||
|
|
||
| Implicit returns are simply a coding style option, and can be used as the final line in a block expression. While early returns require an explicit `return` keyword, any other return can be made implicit according to your preferences or the preferences of your project. In the Rust project itself, the style guidelines lean toward preferring implicit returns. |
This comment has been minimized.
This comment has been minimized.
solson
Jan 8, 2016
Member
I think an explanation of Rust's expression-oriented syntax and value flow would be good for this question. Implicit returns follow naturally from the normal evaluation rules, and mentioning only block expressions doesn't tell the whole story since the final expression might be, say, an if expression which new users might not be aware is an expression at all. Maybe we could link to a place in the book that describes this.
On the style guideline side, you could note the benefit of using return only for early returns, so you know normal control and value flow is being interrupted wherever you see it.
This comment has been minimized.
This comment has been minimized.
tshepang
Jan 8, 2016
Contributor
Yeah, implicit returns are not mere style. They are a strong convention.
This comment has been minimized.
This comment has been minimized.
|
@AndrewBrinker rocks! |
solson
reviewed
Jan 8, 2016
|
|
||
| ### Can I use static values across threads without an `unsafe` block? | ||
|
|
||
| Yes, if the type implements `Sync`, doesn't implement `Drop`, and you don't try to mutate the value. |
This comment has been minimized.
This comment has been minimized.
solson
Jan 8, 2016
Member
Mutation is safe if it's synchronized (e.g. Mutex<T> or AtomicUsize in a static is fine). Currently you need something like the lazy_static crate to initialize a Mutex<T> in a static, but AtomicUsize statics can be created and mutated in stable Rust with just libstd.
solson
reviewed
Jan 8, 2016
|
|
||
| ### How do I do global variables in Rust? | ||
|
|
||
| Globals in Rust can be done using `const` declarations for compile-time computed global constants, while `static` can be used for mutable globals. Note that modifying a `static` variable requires the use of `unsafe`, as it allows for data races, one of the things guaranteed not to happen in safe Rust. One important distinction between `const` and `static` values is that you can take references to `static` values, but not references to `const` values, which don't have a specified memory location. For more information on `const` vs. `static`, read [the Rust book](https://doc.rust-lang.org/book/const-and-static.html). |
This comment has been minimized.
This comment has been minimized.
solson
Jan 8, 2016
Member
A static variable can't be modified even with unsafe - you need static mut, which probably isn't something that should be recommended (particularly to beginners).
The next paragraph is misleading as well since you can't put a RefCell in a static at all currently (static requires Sync). However, one can have memory- and thread-safe mutable globals using the lazy_static crate and Mutex<T> or in simpler cases with a plain static atomic.
solson
reviewed
Jan 8, 2016
|
|
||
| ### Does Rust allow non-constant-expression values for globals? | ||
|
|
||
| No. Globals cannot have a non-constant-expression constructor and cannot have a destructor at all. Static constructors are undesirable because portably ensuring a static initialization order is difficult. Life before main is often considered a misfeature, so Rust does not allow it. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
This is shaping up to be quite excellent! Thanks to everyone who's been working so hard on this. |
alilleybrinker
added some commits
Jan 9, 2016
This comment has been minimized.
This comment has been minimized.
|
Okay, I've now made changes addressing all but the following points of @brson's:
I am also still going through the entire document adding links to the API docs. |
alilleybrinker
added some commits
Jan 9, 2016
This comment has been minimized.
This comment has been minimized.
|
Okay, I've finished adding the links to the API docs, and I've made the correction suggested by @tsion. |
This comment has been minimized.
This comment has been minimized.
I'll take care of merging the styles. I am going to open one PR that contains all pending website changes. |
This comment has been minimized.
This comment has been minimized.
|
My previous points have been addressed to my satisfaction (for the most part, see below), and I'm intending to merge this with the other open PRs and my style changes into one fresh PR for landing. The only remaining big concerns I have about the FAQ are: HKT section, while better, in the first section makes it sound like Rust has HKT by saying that type constructors that exist are KHTs; there's still no Q&A for 'What does "Rust" mean'. I'm happy to address those in follow ups though. |
This comment has been minimized.
This comment has been minimized.
|
Thank you everybody for the amazing reviews and @AndrewBrinker for putting it all together. We're almost done. |
alilleybrinker commentedOct 22, 2015
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.
The 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.
Thoughts? Changes?