std_vs_nostd/
std_vs_nostd.rs

1// Example showing the differences between std and nostd modes
2
3#[cfg(feature = "std")]
4fn main() {
5    use stdpython::*;
6    
7    println!("=== STD MODE EXAMPLE ===");
8    
9    // Standard I/O operations available
10    print("Hello from std mode!");
11    
12    // File operations available
13    match open("example.txt", Some("w")) {
14        Ok(mut file) => {
15            let _ = file.write("Hello, file system!");
16            let _ = file.close();
17            println!("File operations work in std mode");
18        },
19        Err(e) => println!("File error: {}", e),
20    }
21    
22    // All core functions work the same
23    let nums = vec![1, 2, 3, 4, 5];
24    println!("sum([1,2,3,4,5]) = {}", sum(&nums[..]));
25    println!("abs(-42) = {}", abs(-42i64));
26    println!("str(true) = '{}'", str(true));
27    
28    // Collections work
29    let mut list = PyList::from_vec(vec![10, 20, 30]);
30    list.append(40);
31    println!("PyList after append: len = {}", len(&list));
32    
33    println!("=== PyO3 and python-mod integration available ===");
34}
35
36#[cfg(not(feature = "std"))]
37fn main() {
38    extern crate alloc;
39    use alloc::vec;
40    use stdpython::*;
41    
42    println!("=== NO-STD MODE EXAMPLE ===");
43    
44    // No direct printing, but string generation works
45    let output = print_to_string("Hello from nostd mode!");
46    println!("Generated output: {}", output);
47    
48    // File operations not available in nostd mode
49    // This would cause a compile error:
50    // let file = open("test.txt", Some("r"));  // ERROR: not available
51    
52    // All core functions work identically
53    let nums = vec![1, 2, 3, 4, 5];
54    let total = sum(&nums[..]);
55    println!("sum([1,2,3,4,5]) = {}", total);
56    println!("abs(-42) = {}", abs(-42i64));
57    println!("str(true) = '{}'", str(true));
58    
59    // Collections work identically
60    let mut list = PyList::from_vec(vec![10, 20, 30]);
61    list.append(40);
62    println!("PyList after append: len = {}", len(&list));
63    
64    // Generic traits work the same
65    assert_eq!(bool(&list), true);
66    assert_eq!(bool(0i64), false);
67    
68    println!("=== Core Python runtime works without std! ===");
69}