docs: rust: Add error handling sections
authorDirk Behme <dirk.behme@de.bosch.com>
Wed, 15 Jan 2025 06:25:52 +0000 (07:25 +0100)
committerMiguel Ojeda <ojeda@kernel.org>
Sat, 8 Mar 2025 21:52:02 +0000 (22:52 +0100)
Add error handling sections to the documentation and use it
to link to the existing code documentation. This will allow
to extend that documentation, use intra-doc links and test
the examples.

Suggested-by: Miguel Ojeda <ojeda@kernel.org>
Link: https://lore.kernel.org/rust-for-linux/CANiq72keOdXy0LFKk9SzYWwSjiD710v=hQO4xi+5E4xNALa6cA@mail.gmail.com/
Signed-off-by: Dirk Behme <dirk.behme@de.bosch.com>
Link: https://lore.kernel.org/r/20250115062552.1970768-1-dirk.behme@de.bosch.com
[ Slightly tweaked wording. - Miguel ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
Documentation/rust/coding-guidelines.rst
Documentation/rust/testing.rst

index a2e326b42410f8d27f706b4a00e2ad0339be62ac..27f2a7bb5a4a2a12f922ee60a46fb23da11199e3 100644 (file)
@@ -373,3 +373,11 @@ triggered due to non-local changes (such as ``dead_code``).
 For more information about diagnostics in Rust, please see:
 
        https://doc.rust-lang.org/stable/reference/attributes/diagnostics.html
+
+Error handling
+--------------
+
+For some background and guidelines about Rust for Linux specific error handling,
+please see:
+
+       https://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust
index 568b71b415a4525772cb77ce76c7f9da32fac1c4..c33001726e3717389bdb317da25c7ea4e11c63a4 100644 (file)
@@ -123,6 +123,13 @@ A current limitation is that KUnit does not support assertions in other tasks.
 Thus, we presently simply print an error to the kernel log if an assertion
 actually failed. Additionally, doctests are not run for nonpublic functions.
 
+Since these tests are examples, i.e. they are part of the documentation, they
+should generally be written like "real code". Thus, for example, instead of
+using ``unwrap()`` or ``expect()``, use the ``?`` operator. For more background,
+please see:
+
+       https://rust.docs.kernel.org/kernel/error/type.Result.html#error-codes-in-c-and-rust
+
 The ``#[test]`` tests
 ---------------------