StructOp Handbook
Contents Examples Commands
Handbook / Build and test

C and Rust from the same declarations

Keep the target choice in intent.st. Declare values, expressions, functions, modules and abilities once. For a console program the whole intent can be:

intent.st
structop {
    type_app {
        exec = { lang = "Rust" }
    }
}

Change "Rust" to "C" and run structop apply again. The first init locks both core providers. StructOp checks your declarations and generates source in memory for both targets. It builds and runs the selected executable. A C compiler is needed for a C build, and Cargo for a Rust build.

On this page

Shared providers#

The core is implicit when there are no provider entries. For desktop or game programs, list the families you use inside the structop block:

portable desktop providers
lib_provider {
    core = { source = "StructOp/core", version = "@latest" }
    gui  = { source = "StructOp/gui",  version = "@latest" }
}
FamilyC implementationRust implementation
StructOp/corecrust
StructOp/guigtk3gtk-rs
StructOp/gamecursesbracket-lib

For a custom library with matching C and Rust provider APIs, give the entry a paired source. For example, source = { C = "Local/storage-c", Rust = "Local/storage-rs" } lets application code use the same provider abilities. Both implementations must be installed or available to init and expose the attributes and results the application uses.

A version pin applies to both implementations. Use @latest to lock each at its own release version. Run init after changing the provider list. A target-specific project may still list concrete providers directly, including GTK 2 or providers generated from native libraries and crates. Their native APIs can differ.

Writing portable applications#

Write array access as data.value.items.current[i]. Use a sized String destination for text returned by a function. Pointer parameters and checked array indexing work on both targets. The development CLI also supports typed function-local pointers and storing the address of a whole scalar state value on both targets. Rust checks the lifetime of local borrows and rejects storing a local address in program state. Inline C expressions require the C target. Portability validation checks the language and source generation. Executing both binaries still requires their tools and native dependencies.

What you get#

structop apply (Rust)
  # program.app will be created
  + program.app (.structop/build/app)
      + ability.print.Question
      + ability.read_line.Answer
      + ability.print.Greeting

Plan: 1 to add, 0 to change, 0 to destroy.

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Hello, what's your name?
Hello, Ada

.structop/build/main.rs holds the generated source and .structop/build/rust/ the Cargo project it is built in; the program is copied to .structop/build/app like the C one. The source is ordinary, commented Rust, one block per ability; structop apply -source -no-run writes it out for you to read.

How StructOp maps to Rust#

The generated program uses the Rust types and constructs a Rust developer expects, and behaves like the C build of the same declarations.

StructOpRust
Int, Float, Bool, Char, Stringi64, f32, bool, char, String
Array of T, size N / List of T[T; N] / Vec<T>
Pointer to T parameter&mut T
enum / structan enum / a struct
class with methodsa struct with an impl block
interface, implementsa trait and its implementations
constantconst
File variablesstd::fs::File and std::io