بنقرة واحدة
idiomatic-rust
// Idiomatic Rust patterns for pikru C port. Use when writing or reviewing Rust code ported from C. Don't write C in Rust - the goal is correct behavior, not line-by-line translation.
// Idiomatic Rust patterns for pikru C port. Use when writing or reviewing Rust code ported from C. Don't write C in Rust - the goal is correct behavior, not line-by-line translation.
Create minimal subtests to isolate and fix complex bugs. Use when a test fails and the issue is buried in complexity.
Codebase organization for pikru. Use when you need to find where specific functionality lives.
Code annotation requirements for pikru. Use when writing or porting Rust functions from C code. All ported functions must have cref comments.
Debugging conventions for pikru. Use when adding debug traces or investigating issues. Important rule - leave debug traces in place.
Git command conventions for pikru. Use when running any git commands to avoid blocking on interactive pager.
Testing conventions for pikru. Use when running tests to avoid timeouts. DO NOT run the full test suite.
| name | idiomatic-rust |
| description | Idiomatic Rust patterns for pikru C port. Use when writing or reviewing Rust code ported from C. Don't write C in Rust - the goal is correct behavior, not line-by-line translation. |
This is a Rust port of C code, but do not write C in Rust. The goal is correct behavior, not line-by-line translation.
If a struct has all the information needed to compute something, make it a method:
// BAD: C-style function with boolean flag soup
fn text_width(text: &str, charwid: f64, is_mono: bool, font_scale: f64, is_bold: bool) -> f64
// GOOD: The type already knows its properties
impl PositionedText {
fn width_inches(&self, charwid: f64) -> f64 {
// self.mono, self.bold, self.big are all right here
}
}
Rationale: Boolean flags are:
width(s, w, true, 1.0, false) - which bool is which?)// BAD: Bare f64 everywhere, easy to mix up inches and pixels
fn convert(value: f64, scale: f64) -> f64
// GOOD: The type system catches mistakes
struct Inches(f64);
struct Pixels(f64);
fn convert(value: Inches, scale: &Scaler) -> Pixels
When you find yourself writing foo_bar(bar, ...), consider bar.foo(...) instead. This:
// BAD: What does `true` mean here?
render_text(text, true, false)
// GOOD: Self-documenting
enum TextAnchor { Start, Middle, End }
render_text(text, TextAnchor::Start)
When porting C:
The // cref: comments link to C for reference, but the Rust should stand on its own.