rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Leandro Coutinho <lescoutinhovr@gmail.com>
To: Laight@aculab.com
Cc: peterz@infradead.org, rust-for-linux@vger.kernel.org
Subject: RE: [PATCH 00/13] [RFC] Rust support
Date: Sun, 9 May 2021 18:28:49 -0300	[thread overview]
Message-ID: <CAN6UTazQwN=oZutVvRio1ZoXBMDm4DXNnqEnZJ_f4fD7vFqFPA@mail.gmail.com> (raw)

> > The more you make it look like (Kernel) C, the easier it is for us C
> > people to actually read. My eyes have been reading C for almost 30 years
> > by now, they have a lexer built in the optical nerve; reading something
> > that looks vaguely like C but is definitely not C is an utterly painful
> > experience.
>
> I'll see your 30 years and raise to over 35.
> (And writing code that accesses hardware for 6 or 7 years before that.)
>
> Both Java and go can look more like the K&R style C than any of the
> examples from microsoft - which seem to utilise as much vertical space
> as humanly? possible.
>
> Those rust examples seemed to be of the horrid microsoft sytle.
> Nothing about that style makes reading code easy.
>
>   David

One thing I miss is the good old for loop.

Rust `for` works fine, until the step is not a simple unsigned int, eg:
for (int i = n / 2; i > 0; i /= 2)

In Rust you do: (please let me know if there is a better way):
    let mut i = n / 2;
    while i > 0 {
        // some logic ...
        i /= 2;
    }

The great thing about the C 'for' loop is initialization, condition and
step at the same line makes it very easy to understand the code, and
less error prone, like forgetting the step at the end of the loop.

Some code if people would like to test too:

```
// https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.step_by

fn main() {

    let n: i32 = 10;
    // for (int i = n / 2; i > 0; i /= 2)
    let mut i = n / 2;
    while i > 0 {
        print!("{} ", i);
        i /= 2;
    }
    println!();

    // or ...
    i = n / 2;
    loop {
        if i == 0 { break; }
        print!("{} ", i);
        i /= 2;
    }
    println!();

    // i = i == 0 ? n >> 2 : n / 2; // nope. Rust does not have it ... :/
    i = if i == 0 { n >> 2 } else { n / 2};

    // for (int i = n >> 2; i > 0; i = i >> 2)
    while i > 0 {
        // some logic ...
        print!("{} ", i);

        i = i >> 2;
    }
    println!();

    for i in 0..4 { print!("{} ", i); }
    println!();
    for i in (0..4).step_by(2) { print!("{} ", i); }
    println!();
    for i in (0..4).rev() { print!("{} ", i); }
    println!();
    for i in (0..4).rev().step_by(2) { print!("{} ", i); }
    println!();
}
```

But Rust has many nice features.
I hope it works. :)

             reply	other threads:[~2021-05-09 21:29 UTC|newest]

Thread overview: 144+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-09 21:28 Leandro Coutinho [this message]
2021-05-09 21:49 ` [PATCH 00/13] [RFC] Rust support Greg Morenz
2021-05-09 21:55   ` Leandro Coutinho
  -- strict thread matches above, loose matches on Subject: below --
2021-07-30 23:22 Dillan Jackson
2021-04-29  5:21 Mariusz Ceier
2021-04-29  8:18 ` David Laight
2021-04-29  5:20 Mariusz Ceier
2021-04-14 18:45 ojeda
2021-04-14 19:44 ` Linus Torvalds
2021-04-14 20:20   ` Miguel Ojeda
2021-04-15  1:38     ` Kees Cook
2021-04-15  8:26       ` David Laight
2021-04-15 18:08         ` Kees Cook
2021-04-15 12:39       ` Miguel Ojeda
2021-04-14 20:09 ` Matthew Wilcox
2021-04-14 20:21   ` Linus Torvalds
2021-04-14 20:35     ` Josh Triplett
2021-04-14 22:08     ` David Laight
2021-04-14 20:29   ` Miguel Ojeda
2021-04-18 15:31   ` Wedson Almeida Filho
2021-04-15  0:22 ` Nick Desaulniers
2021-04-15 10:05   ` Miguel Ojeda
2021-04-15 18:58 ` Peter Zijlstra
2021-04-16  2:22   ` Wedson Almeida Filho
2021-04-16  4:25     ` Al Viro
2021-04-16  5:02       ` Wedson Almeida Filho
2021-04-16  5:39         ` Paul Zimmerman
2021-04-16  7:46         ` Peter Zijlstra
2021-04-16  7:09     ` Peter Zijlstra
2021-04-17  5:23       ` comex
2021-04-17 12:46       ` David Laight
2021-04-17 14:51       ` Paolo Bonzini
2021-04-19  7:32         ` Peter Zijlstra
2021-04-19  7:53           ` Paolo Bonzini
2021-04-19  8:26             ` Peter Zijlstra
2021-04-19  8:35               ` Peter Zijlstra
2021-04-19  9:02               ` Paolo Bonzini
2021-04-19  9:36                 ` Peter Zijlstra
2021-04-19  9:40                   ` Paolo Bonzini
2021-04-19 11:01                     ` Will Deacon
2021-04-19 17:14                   ` Linus Torvalds
2021-04-19 18:38                     ` Paolo Bonzini
2021-04-19 18:50                       ` Linus Torvalds
2021-04-22 10:03     ` Linus Walleij
2021-04-22 14:09       ` David Laight
2021-04-22 15:24       ` Wedson Almeida Filho
2021-04-26  0:18         ` Linus Walleij
2021-04-26 14:26           ` Miguel Ojeda
2021-04-26 14:40           ` Wedson Almeida Filho
2021-04-26 16:03             ` Miguel Ojeda
2021-04-27 10:54             ` Linus Walleij
2021-04-27 11:13               ` Robin Randhawa
2021-04-29  1:52               ` Wedson Almeida Filho
2021-04-26 18:01           ` Miguel Ojeda
2021-04-22 21:28       ` Miguel Ojeda
2021-04-26  0:31         ` Linus Walleij
2021-04-26 18:18           ` Miguel Ojeda
2021-04-27 11:13             ` Linus Walleij
2021-04-28  2:51               ` Kyle Strand
2021-04-28  3:10               ` Miguel Ojeda
2021-05-04 21:21                 ` Linus Walleij
2021-05-04 23:30                   ` Miguel Ojeda
2021-05-05 11:34                     ` Linus Walleij
2021-05-05 14:17                       ` Miguel Ojeda
2021-05-05 15:13                         ` Enrico Weigelt, metux IT consult
2021-05-06 12:47                         ` Linus Walleij
2021-05-07 18:23                           ` Miguel Ojeda
2021-04-16  4:27   ` Boqun Feng
2021-04-16  6:04     ` Nick Desaulniers
2021-04-16 18:47       ` Paul E. McKenney
2021-04-19 20:35         ` Nick Desaulniers
2021-04-19 21:37           ` Paul E. McKenney
2021-04-19 22:03           ` Miguel Ojeda
2021-04-16 20:48     ` Josh Triplett
2021-04-16  8:16   ` Michal Kubecek
2021-04-16  9:29     ` Willy Tarreau
2021-04-16 11:24 ` Peter Zijlstra
2021-04-16 13:07   ` Wedson Almeida Filho
2021-04-16 14:19     ` Peter Zijlstra
2021-04-16 15:04       ` Miguel Ojeda
2021-04-16 15:43         ` Peter Zijlstra
2021-04-16 16:21           ` Miguel Ojeda
2021-04-16 15:33       ` Wedson Almeida Filho
2021-04-16 16:14         ` Willy Tarreau
2021-04-16 17:10           ` Miguel Ojeda
2021-04-16 17:18             ` Peter Zijlstra
2021-04-16 18:08               ` Matthew Wilcox
2021-04-17 11:17                 ` Peter Zijlstra
2021-04-17 11:46                   ` Willy Tarreau
2021-04-17 14:24                     ` Peter Zijlstra
2021-04-17 14:36                       ` Willy Tarreau
2021-04-17 13:46                   ` David Laight
2021-04-16 17:37             ` Willy Tarreau
2021-04-16 17:46               ` Connor Kuehl
2021-04-20  0:24               ` Nick Desaulniers
2021-04-20  3:47                 ` Willy Tarreau
2021-04-20  5:56                 ` Greg Kroah-Hartman
2021-04-20  6:16                   ` Willy Tarreau
2021-04-29 15:38                     ` peter enderborg
2021-04-17 13:53           ` Wedson Almeida Filho
2021-04-17 14:21             ` Willy Tarreau
2021-04-17 15:23               ` Miguel Ojeda
2021-04-18 15:51               ` Wedson Almeida Filho
2021-04-17 12:41       ` David Laight
2021-04-17 13:01         ` Wedson Almeida Filho
2021-04-16 15:03     ` Matthew Wilcox
2021-04-17 13:29       ` Wedson Almeida Filho
2021-04-16 15:58     ` Theodore Ts'o
2021-04-16 16:21       ` Wedson Almeida Filho
2021-04-17 15:11       ` Paolo Bonzini
2021-04-16 14:21   ` Miguel Ojeda
2021-04-17 20:42 ` Richard Weinberger
2021-04-28 18:34 ` Mariusz Ceier
2021-04-28 20:25   ` Nick Desaulniers
2021-04-28 21:21   ` David Laight
2021-04-29 11:14     ` Kajetan Puchalski
2021-04-29 11:25   ` Kajetan Puchalski
2021-04-29 14:06     ` Mariusz Ceier
2021-04-29 14:13       ` Sven Van Asbroeck
2021-04-29 14:26         ` Willy Tarreau
2021-04-29 15:06       ` Al Viro
2021-04-29 16:09         ` Mariusz Ceier
2021-04-30  6:39     ` Thomas Schoebel-Theuer
2021-04-30  8:30       ` David Laight
2021-05-05 13:58       ` Enrico Weigelt, metux IT consult
2021-05-05 14:41         ` Miguel Ojeda
2022-06-20 15:11 ` Olliver Schinagl
2022-06-27 17:44   ` Miguel Ojeda
2022-07-18  6:56     ` Olliver Schinagl
2022-07-20 19:23       ` Miguel Ojeda
2022-07-20 20:21         ` Nicolas Pitre
2022-07-27  7:47           ` Olliver Schinagl
2022-07-27 13:32             ` Nicolas Pitre
2022-07-27  8:05         ` Olliver Schinagl
2022-07-28 10:21           ` Gary Guo
2022-07-28 12:09             ` Greg Kroah-Hartman
2022-07-28 12:28               ` Gary Guo
2022-07-28 20:45               ` Olliver Schinagl
2022-07-29  8:04                 ` Greg Kroah-Hartman
2022-07-28 20:43             ` Olliver Schinagl
2022-10-15 14:16               ` Olliver Schinagl
2022-10-16  1:44                 ` Bagas Sanjaya
2022-10-16  1:50                   ` Bagas Sanjaya
2022-10-16 13:23                 ` Josh Triplett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN6UTazQwN=oZutVvRio1ZoXBMDm4DXNnqEnZJ_f4fD7vFqFPA@mail.gmail.com' \
    --to=lescoutinhovr@gmail.com \
    --cc=Laight@aculab.com \
    --cc=peterz@infradead.org \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).