Hi,
Reading this page: https://doc.rust-lang.org/rust-by-example/fn/methods.html
Is it possible to embed the implementation in the sub?
What are the practical consequences?
Larger build time?
Thanks
Hi,
Reading this page: https://doc.rust-lang.org/rust-by-example/fn/methods.html
Is it possible to embed the implementation in the sub?
What are the practical consequences?
Larger build time?
Thanks
What is a "sub"?
If you are meaning "put the methods inside the struct definition like other languages" then the biggest reasons to have impl
blocks separate from the struct
declaration are
struct Point<T, const N: usize>([T; N])
, I might want to add a method for the dot product when N
is 2... impl<T: Add+Mul> Point<T, 2> { fn dot(...) -> T { ... } }
)The difference is almost entirely related to developer ergonomics, and it doesn't impact build times nearly as much as other decisions the Rust language designers made (monomorphisation, macros, type system is turing complete, etc.).
I feel really sorry, I wrote on a smartphone and didn't see the mistake.
I meant struct.
In C++ it has implications on Translation Units ==> longer building times.
So if Rust doesn't have this limitation, that's a good thing..
Thanks!
This language is starting to make sense
FYI: in Rust the compilation unit is an entire crate.
The compiler can reuse some work when recompiling a crate ("incremental compilation") but for the most part, how you organize code within a crate will not affect the characteristics of the compilation process.
Wow, that's great news actually.
Really, really good
Yeah, it makes different trade-offs with different pros/cons.
For example, it means importing code from a different module has no effect on build times other than adding another entry to your "current namespace" hashmap. It also means you don't need to re-parse and re-typecheck the definition for a struct for every file in your project that uses it.
However, it does mean changing one file means you need to rebuild the entire crate, although this is largely offset by the awesome work done by the Rust compiler team to cache all the fine-grained analysis and object files generated from previous runs.
Do you mean that if a crate imports 10 modules and I do only change one of them, I could have to rebuild the whole crate, except if the compiler detects that changes only occurred in that file and only recompile that module?
Have a look at the "Queries" section in the rustc dev guide - it details how the Rust compiler is implemented internally much better than I could explain in a single post.
Thanks for the pointer Michael, I will read it now.
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.