rustlings | Small exercises to get you used to reading and writing Rust
kandi X-RAY | rustlings Summary
kandi X-RAY | rustlings Summary
Greetings and welcome to rustlings. This project contains small exercises to get you used to reading and writing Rust code. This includes reading and responding to compiler messages!.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of rustlings
rustlings Key Features
rustlings Examples and Code Snippets
Community Discussions
Trending Discussions on rustlings
QUESTION
When I do exercise in rustlings, I found that exercise:
...ANSWER
Answered 2022-Feb-20 at 14:55Let's substitute the inner &str
with T
. Then &[T]
is a slice of T
and &Vec
is a reference to a vector of T
.
When a function is defined to accept a &[T]
, you can also pass a &Vec
. Another very common case is to declare a parameter as &str
, as that allows you to directly pass, for example &String
. This is known as Deref
coercion.
QUESTION
I'm going thru the rustlings exercises but I don't seem to understand how borrow semantics works.
I created this simple example (check the playground):
...ANSWER
Answered 2022-Jan-25 at 22:10You can't have two mutable references to the same value at the same time. If you reorder the lines in your function like this:
QUESTION
I'm learning rust and I'm currently working on the exercise here.
So far I've come up with the following:
...ANSWER
Answered 2022-Jan-02 at 06:29map_err
is returning a Result
but you're trying to affect it to x
, which you declared as an i64
variable. You need to add a ?
so that an Err
result will be returned and an Ok
result will be unwrapped: let x = s.parse::().map_err(ParsePosNonzeroError::from_parseint)?;
QUESTION
I'm doing the rustlings exercises and I tried this to make a capitalize function. But the join part does not work. It says:
"the method join
exists for struct Vec
, but its trait bounds were not satisfied
the following trait bounds were not satisfied:
<[char] as Join<_>>::Output = _
"
which I don't know what means. What would be the right way to join a char vector?
...ANSWER
Answered 2021-Nov-20 at 23:51There's a constraint in the return type of the join
method that the char
type does not meet, since it doesn't have a static lifetime:
QUESTION
In the rustlings, in `iterators2.rs', I need to capitalize the first letter of a word.
This is the code that I have
...ANSWER
Answered 2021-Nov-14 at 01:07The reason why is that we are in a big world and Rust strings are encoded in UTF-8, allowing for different languages and characters to be encoded correctly. In some languages, a lowercase letter might be one character, but its uppercase form is two characters.
For this reason, char.to_uppercase()
return an iterator that must be collected to get the appropriate result.
Some(first) => first.to_uppercase().collect()
should fix this issue
QUESTION
I'm trying to do the rustlings course and I don't understand the error I'm getting for the following code:
...ANSWER
Answered 2021-Oct-20 at 09:22In rust only the last expression is taken as return value.
In your case:
QUESTION
I am currently making my way through the Rust book and rustlings. try_from_into.rs
asks us to implement TryFrom
for a tuple, and array, and a slice. The array and slice versions fit nicely into an iter/map/collect
pattern, e.g. as follows:
ANSWER
Answered 2021-Oct-16 at 00:55You could change the match
block to a ?
if you map the error into an IntoColorError
. That will allow you to continue chaining methods so you can call try_into()
to convert the Vec
into [u8; 3]
, which can in turn be destructured into separate red
, green
, and blue
variables.
QUESTION
I'm currently reading the official rust-lang book (the one on their website/documentation) and I'm taking notes by copying code and writing comments for everything. I'm currently on chapter 6, Options enum type. Based on the book and some Rustlings code I came across while googling the following should be possible based on the the official book
...ANSWER
Answered 2021-Sep-28 at 23:42It appears that you have defined your own enum called Option
in your program. Thus, there are two different types called Option
: yours (main::Option
), and the standard one (std::option::Option
). The variable none
has type main::Option
, but None
is of type std::option::Option
.
Obvious solution is to just delete your own enum. If, however, for the sake of experiment you do want to create an instance of your own enum called Option
, and assign the value of None
to it, you'd need to qualify None
:
QUESTION
When doing rustlings standard_library_types/iterators2.rs
, I started wondering how std::iter::Iterator::map
calls its argument closure/function. More specifically, suppose I have a function
ANSWER
Answered 2021-Jun-07 at 02:02Why can I call capitalize_first
with a &&str
argument?
The linked Q&A for auto-dereferencing rules is specifically for how self
is resolved when using the a.b()
syntax. The rules for arguments in general skip the auto-reference step and just rely on Deref coercions. Since &&str
implements Deref
(and indeed all references implement Deref
), this &&str
-> &str
transformation happens transparently.
Why doesn't it work for .map()
then?
Plain and simply, map()
is expecting something that implements Fn(&&str) -> T
and capitalize_first
does not. A Fn(&str)
is not transparently transformed into a Fn(&&str)
, it requires a transformation step like the one introduced by the closure (albeit transparently).
QUESTION
I'm currently working my way through the rustlings course and got confused when this exercise regarding move-semantics came up. Here is the gist of it:
...ANSWER
Answered 2021-Apr-16 at 06:55is the same pointer reused and just made mutable?
Yes. Well the same object entirely, at runtime it's essentially a no-op.
Why would shadowing be used in this case at all?
Some people like the pattern of temporally "locking" and "unlocking" bindings via shadowing e.g.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rustlings
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page