Can any rustaceans give me an example of a "higher step" in rust would be?
Can any rustaceans give me an example of a "higher step" in rust would be?
https://doc.rust-lang.org/std/rc/struct.Rc.html
https://doc.rust-lang.org/rust-by-example/std/arc.html
Until it clicks and starts to be natural, there might be a quite rough period.
Until you know the essential traits, how to find them, and you're used to clauses on generics, you may also have some difficulties making sense of some generic code or making your own generic code work.
What I find way harder to cope with is the gazillions of type parameters with gazillions of guards, send, sync, clone or not clone, then impl Foo for Bar vs impl Foo for Arc<Bar> etc etc; and tons of features in cargo crates, also in private workspace projects, .... And the compiler slowness oh man the compiler slowness.
EDIT: my life improved a bit since I learned about the cargo hakari plugin
But there needs to be more in the way of education that helps you understand the types of designs that the compiler is trying to push you towards.
You can generally get past things by simply `.unwrap()`ing and `.clone()`ing your way past compiler issues, but reaching for them too often prevents you from ever learning those (important) lessons given the current lack of better alternatives to learning them.
> So before you dive, have an overview of the book (or books). Maybe don't read it fully, but make sure to have read all titles so that you know when to come back to it later.
Could someone familiar with Solana smart contract development highlight which titles from 'the book' are most relevant? I imagine developing smart contracts is much more scoped down than most rust projects.
I have a `Span` type and it can act like `RangeInclusive`. I want to index into a `&str` with it. This looks something like this :
use std::ops::Index;
pub struct Span {
start:usize,
end:usize,
}
impl<'a> Index<Span> for &'a str {
type Output = str;
fn index(&self, span:Span) -> &Self::Output {
&self[span.start..=span.end]
}
}
(NB4 RangeInclusive, in this example, Span is almost identical to RangeInclusive, in practice it was not)Playground: https://play.rust-lang.org/?version=stable&mode=debug&editio...
This gives you a cryptic error :
error[E0308]: mismatched types
--> src/lib.rs:10:19
|
10 | &self[span.start..=span.end]
| ^^^^^^^^^^^^^^^^^^^^^ expected struct `Span`, found struct `RangeInclusive`
|
= note: expected struct `Span`
found struct `RangeInclusive<usize>`
For more information about this error, try `rustc --explain E0308
This is weird, because you can definitely index into a `&str` with `RangeInclusive`.What you actually need to do is implement it for `str` instead of `&'a str`. This is a little peculiarity with how the `str` type works (to date, this is the only time I've ever needed the raw `str` type and not &str! it almost never is required) and the generic implementation for Index<I> for str combined with how Deref works.
It's a small one line change to fix the code, but it's not obvious from the error message how to do it, and a bit tricky to solve unless you knew a little bit more about how Rust's operators get parsed/implemented.
In my experience this is a common class of problems when you deal with generic and trait programming in Rust. It's ultimately the front end to an advanced constraint solver, and due to the explosion of complexity when you start making generic things more/less generic you will hit the wall with weirdness and need to understand more about the type system rather than letting the type system prevent you from doing stupid things.
Frankly the memory management issue is not a problem once you buy into ownership.