Skip to content

Commit 892657d

Browse files
committed
deploy: 08e15b4
1 parent 40e35c5 commit 892657d

12 files changed

Lines changed: 1498 additions & 956 deletions

_sources/coding-guidelines/associated-items.rst.txt

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,63 +30,67 @@ Associated Items
3030

3131
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.
3232

33-
.. code-block:: rust
33+
.. rust-example::
3434

3535
// 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.
42-
fn concat_strings(input: &[MyEnum]) -> String {
43-
let mut result = String::new();
44-
for item in input {
45-
match item {
46-
MyEnum::Str(s) => result.push_str(s),
47-
MyEnum::List(list) => result.push_str(&concat_strings(list)),
48-
}
49-
}
50-
result
51-
}
36+
enum MyEnum {
37+
Str(String),
38+
List(Vec<MyEnum>),
39+
}
40+
41+
// Concatenates strings from a nested structure of `MyEnum` using recursion.
42+
fn concat_strings(input: &[MyEnum]) -> String {
43+
let mut result = String::new();
44+
for item in input {
45+
match item {
46+
MyEnum::Str(s) => result.push_str(s),
47+
MyEnum::List(list) => result.push_str(&concat_strings(list)),
48+
}
49+
}
50+
result
51+
}
52+
#
53+
# fn main() {}
5254

5355
.. compliant_example::
5456
:id: compl_ex_9pK3h65rfceO
5557
:status: draft
5658

5759
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.
5860

59-
.. code-block:: rust
61+
.. rust-example::
6062

6163
// 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`.
69-
fn concat_strings_non_recursive(input: &[MyEnum]) -> Result<String, &'static str> {
70-
const MAX_STACK_SIZE: usize = 1000;
71-
let mut result = String::new();
72-
let mut stack = Vec::new();
73-
74-
// Add all items to the stack
75-
stack.extend(input.iter());
76-
77-
while let Some(item) = stack.pop() {
78-
match item {
79-
MyEnum::Str(s) => result.insert_str(0, s),
80-
MyEnum::List(list) => {
81-
// Add list items to the stack
82-
for sub_item in list.iter() {
83-
stack.push(sub_item);
84-
if stack.len() > MAX_STACK_SIZE {
85-
return Err("Too big structure");
86-
}
87-
}
88-
}
89-
}
90-
}
91-
Ok(result)
92-
}
64+
enum MyEnum {
65+
Str(String),
66+
List(Vec<MyEnum>),
67+
}
68+
69+
/// Concatenates strings from a nested structure of `MyEnum` without using recursion.
70+
/// Returns an error if the stack size exceeds `MAX_STACK_SIZE`.
71+
fn concat_strings_non_recursive(input: &[MyEnum]) -> Result<String, &'static str> {
72+
const MAX_STACK_SIZE: usize = 1000;
73+
let mut result = String::new();
74+
let mut stack = Vec::new();
75+
76+
// Add all items to the stack
77+
stack.extend(input.iter());
78+
79+
while let Some(item) = stack.pop() {
80+
match item {
81+
MyEnum::Str(s) => result.insert_str(0, s),
82+
MyEnum::List(list) => {
83+
// Add list items to the stack
84+
for sub_item in list.iter() {
85+
stack.push(sub_item);
86+
if stack.len() > MAX_STACK_SIZE {
87+
return Err("Too big structure");
88+
}
89+
}
90+
}
91+
}
92+
}
93+
Ok(result)
94+
}
95+
#
96+
# fn main() {}

0 commit comments

Comments
 (0)