Bobinas P4G
  • Login
  • Public

    • Public
    • Groups
    • Popular
    • People

// Assumes b != c fn index_two<T>(a:& mut[T], b:usize, c:usize) -> (&mut T, &mut T) { let ordered = b < c; let (low_idx, high_idx) = if ordered { (b,c) } else { (c,b) }; let (low_slice, high_slice) = a.split_at_mut(high_idx); let (low, high) = (&mut low_slice[low_idx], &mut high_slice[0]); return if ordered { (low, high) } else { (high, low) } } // Usage: let (column_b, column_c) = index_two(&mut data, b-1, c-1);

Download link

https://files.mastodon.social/media_attachments/files/109/464/849/437/818/225/original/57fa8816e0da2bce.png

Notices where this attachment appears

  1. mcc (mcc@mastodon.social)'s status on Tuesday, 06-Dec-2022 08:05:57 UTC mcc mcc

    I find myself making good progress with Rust, but also, every so often I have to do something frighteningly high-level to do something simple. For example, it turns out when you access an array member, it locks the *entire array* for mutation. So if you want to put two items A and B, from the array into variables and have both be mutable, you… can't.

    Unless you do this very complicated thing with "split_at_mut". Which works. But would someone new to programming have been able to figure it out?

    In conversation Tuesday, 06-Dec-2022 08:05:57 UTC from mastodon.social permalink
  • Help
  • About
  • FAQ
  • Privacy
  • Source
  • Version
  • Contact

Bobinas P4G is a social network. It runs on GNU social, version 2.0.1-beta0, available under the GNU Affero General Public License.

Creative Commons Attribution 3.0 All Bobinas P4G content and data are available under the Creative Commons Attribution 3.0 license.