You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: _sources/coding-guidelines/associated-items.rst.txt
+53-49Lines changed: 53 additions & 49 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,63 +30,67 @@ Associated Items
30
30
31
31
The below function ``concat_strings`` is not complaint because it call itself and depending on depth of data provided as input it could generate an stack overflow exception or undefine behavior.
32
32
33
-
.. code-block:: rust
33
+
.. rust-example::
34
34
35
35
// Recursive enum to represent a string or a list of `MyEnum`
36
-
enum MyEnum {
37
-
Str(String),
38
-
List(Vec<MyEnum>),
39
-
}
40
-
41
-
// Concatenates strings from a nested structure of `MyEnum` using recursion.
The following code implements the same functionality using iteration instead of recursion. The ``stack`` variable is used to maintain the processing context at each step of the loop. This approach provides explicit control over memory usage. If the stack grows beyond a predefined limit due to the structure or size of the input, the function returns an error rather than risking a stack overflow or out-of-memory exception. This ensures more predictable and robust behavior in resource-constrained environments.
58
60
59
-
.. code-block:: rust
61
+
.. rust-example::
60
62
61
63
// Recursive enum to represent a string or a list of `MyEnum`
62
-
enum MyEnum {
63
-
Str(String),
64
-
List(Vec<MyEnum>),
65
-
}
66
-
67
-
/// Concatenates strings from a nested structure of `MyEnum` without using recursion.
68
-
/// Returns an error if the stack size exceeds `MAX_STACK_SIZE`.
0 commit comments