[[!tag learning-rust]]
New thing: modules. Mostly separate from source code files: a file can contain any number of modules.
Names in a module, or struct, are private by default, and must be declared with
pub
to be public. I like. It's much better than C (public by default) or Python (only a convention that a leading underscore makes a name private; the language doesn't enforce this).This kind of thing is the more helpful to larger a code base becomes, and the more code is shared between developers.
Also, a struct itself can be public, while it's members are not. Another good idea. Within a module struct members are public. This seems convenient, but it strikes me that keeping modules small is going to be a good idea.
A file
src/foo.rs
is a module,mod foo;
is needed to use it.I like that rustc (and cargo) handles building of modules automatically, and that not Makefiles are needed.
Summary of how modules and separate source files work:
Main function is in
main.rs
. It containspub mod foo { pub fn answer() ... }
, to define a module in the same file.File
bar.rs
containspub fn answer()...
. Can be used frommain.rs
withmod bar; bar::answer()
File
yo/mod.rs
containspub fn answer()...
. Can be used frommain.rs
withmod yo; yo::answer()
. Note thatmod.rs
is the required filename.File
yo/thing.rs
containspub fn answer()...
andyo/mod.rs
containspub mod thing;
. Can be used frommain.rs
withmod yo; yo::thing::answer()
.
rustc can handle all of this. It builds everything from scratch by default, which is fine for me for now. Rustc can build libraries (called crates) as well, reducing the need to build everything every time.
Use
extern foo;
to use a separately built library.Static linking is the default, at least for now. Makes for easier development of Rust, since there's no need for a stable ABI, and no need to rebuild everything after each Rust stable release. However, I expect Debian to favour dynamic linking, but I haven't checked.
I can live with static linking, but it's awkward for security updates.
The author says Go has a philosophical problem with dynamic linking. I wonder what that is.
Cargo is the Rust workflow tool and package manager. https://crates.io/ is the site for sharing Rust libraries. I'll want to see what security guarantees they provide.
New thing: raw string literals:
r#"..."#
(allows embeded newlines, and backslashes lose meaning).Will need to think about how Cargo deals with versioned depenedcies.
I'll need to experiment with the regex and crono crates at some point. No hurry, though. I'll wait until I need them. I will probably want to play with the serde crates soon, though. They seem much likely to be useful soon in ick.