Rust 101 – 18: Dependencies and Cargo.toml

How to describe details of your Rust project with a Cargo.toml file, and how to find and add dependencies (other people’s code).

Series: 1: Intro, 2: Language basics, 3: Memory and ownership, 4: Exercises A1, 5: References, 6: Structs and Enums, 7: Panic and Result, 8: Methods, 9: Vec and Box, 10: Strings, 11: Exercises A2, 12: Traits, 13: Type Params, 14: std Traits, 15: Lifetimes, 16: Exercises A3pt1, 17: Exercises A3pt2, 18: Dependencies

Links:

The course materials for this series are developed by tweede golf. You can find more information at github.com/tweedegolf/101-rs and you can sponsor the work at github.com/sponsors/tweedegolf. They are released under the Creative Commons Attribution Share Alike 4.0 International license.

This series of videos is copyright 2024 Andy Balaam and the tweede golf contributors and is released under the Creative Commons Attribution Share Alike 4.0 International license.

Rust 101 – 17: Exercises for module A3 (part 2)

Finishing off the exercises on Rust traits, designing a customised version of Vec.

Series: 1: Intro, 2: Language basics, 3: Memory and ownership, 4: Exercises A1, 5: References, 6: Structs and Enums, 7: Panic and Result, 8: Methods, 9: Vec and Box, 10: Strings, 11: Exercises A2, 12: Traits, 13: Type Params, 14: std Traits, 15: Lifetimes, 16: Exercises A3pt1, 17: Exercises A3pt2, 18: Dependencies

Links:

The course materials for this series are developed by tweede golf. You can find more information at github.com/tweedegolf/101-rs and you can sponsor the work at github.com/sponsors/tweedegolf. They are released under the Creative Commons Attribution Share Alike 4.0 International license.

This series of videos is copyright 2024 Andy Balaam and the tweede golf contributors and is released under the Creative Commons Attribution Share Alike 4.0 International license.

Automated backups from Signal to Nextcloud

DON’T DO THIS: the Signal app has the ability to make daily backups of your messages, and when you choose a folder to backup into, you can choose “Nextcloud” as the device, then choose a Nextcloud folder to back up to.

Don’t do that.

Although the above worked for me for a long time, a few weeks ago, Signal started making 0-byte files for all my backups, and merrily deleting the old ones.

Instead, choose a normal folder on your phone and ask Signal to back up to there.

Then open the Nextcloud app, go to Settings, Auto-uploads, and add a custom folder to auto-upload. For Local Folder, choose the folder you just told Signal to back up into, and for the Remote Folder choose somewhere in Nextcloud to store your backups.

Nextcloud will automatically notice whenever a new backup file appears and copy it to your server.

Rust 101 – 16: Exercises for module A3 (part 1)

Going through some exercises on Rust traits, designing a customised version of Vec.

Series: 1: Intro, 2: Language basics, 3: Memory and ownership, 4: Exercises A1, 5: References, 6: Structs and Enums, 7: Panic and Result, 8: Methods, 9: Vec and Box, 10: Strings, 11: Exercises A2, 12: Traits, 13: Type Params, 14: std Traits, 15: Lifetimes, 16: Exercises A3pt1, 17: Exercises A3pt2, 18: Dependencies

Links:

The course materials for this series are developed by tweede golf. You can find more information at github.com/tweedegolf/101-rs and you can sponsor the work at github.com/sponsors/tweedegolf. They are released under the Creative Commons Attribution Share Alike 4.0 International license.

This series of videos is copyright 2024 Andy Balaam and the tweede golf contributors and is released under the Creative Commons Attribution Share Alike 4.0 International license.

One import per line is best

Rust has a feature where if you import two things from the same module you can abbreviate it like this:

use mypkg::{MyStruct1, MyStruct2};

If you prefer, you can keep them separate, like this:

use mypkg::MyStruct1;
use mypkg::MyStruct2;

I do prefer. Strongly.

Advantages of the abbreviated style

  • Fewer lines of code
  • Fewer characters of code

Advantages of the separated style

  • Simpler diffs with fewer conflicts. If I add or delete an import, it adds or removes exactly one line. This makes it easier to read diffs and reduces conflicts when merging.
  • Less diff noise when lines overflow. Adding or removing an import is less likely to cause the code formatter to reflow the line. Reflows make it hard to see which dependencies have changed when reading a diff.
  • Easier searching. If I’m trying to find out where a particular type is used I can search for its full path, and my search will find all the places I import it.
  • Easier to read. I find line of code like this really hard to read:
    use crate::{
        gossiping::{GossipRequest, GossippedSecret, SecretInfo},
        identities::{ReadOnlyDevice, ReadOnlyUserIdentities},
        olm::{OutboundGroupSession, PrivateCrossSigningIdentity},
        types::events::room_key_withheld::RoomKeyWithheldEvent,
        TrackedUser,
    };

I think the separated style is much better, and I wish Rustfmt defaulted to using it. What do you think?

rustfmt config

To enforce my preferred style, include this in your rustfmt.toml:

imports_granularity = Item

(Rustfmt docs)