Rust and .NET, both directions

Ruststudio packs a Rust library as a NuGet package with a generated C# wrapper, so a .NET project can call it after dotnet add package, and it restores NuGet packages with native libraries into Rust projects.

Updated · describes Ruststudio 0.1.7

Ruststudio editing a Rust project: file explorer, syntax-highlighted code, run and build toolbar, status bar
Ruststudio editing a Rust project: file explorer, syntax-highlighted code, run and build toolbar, status bar

Pack a Rust library as a NuGet package that C# developers can add with one command, and restore NuGet native libraries into your Rust solution.

What you get

Pack

Rust library to NuGet

Reads your extern "C" functions, builds in release, generates a C# class and writes a .nupkg with the native library for each platform.

Wrapper

Generated C# P/Invoke

Integers, floats, bool, strings and pointers are mapped for you. Returned strings are freed through your library's free function.

Use

NuGet packages in Rust

Search nuget.org, restore packages with native libraries into the solution, and add them to build.rs in one click.

Template

Rust library for .NET

A cdylib starter with an add function, a string function and a free function, ready to pack.

Try it

Try NuGet for Rust and .NET

Turn extern "C" functions into a C# wrapper and a package, and use native NuGet packages in Rust.

Ruststudio · NuGetInteractive · simulated

src/lib.rs (edit me)

Platforms already built

Native.cs (generated: 4 functions)

// <auto-generated> by Ruststudio from the extern "C" functions in lib.rs
using System;
using System.Runtime.InteropServices;

namespace MathLib;

public static class Native
{
    private const string Lib = "mathlib";

    [DllImport(Lib, EntryPoint = "add", CallingConvention = CallingConvention.Cdecl)]
    private static extern int add_raw(int a, int b);
    public static int Add(int a, int b) => add_raw(a, b);

    [DllImport(Lib, EntryPoint = "scale", CallingConvention = CallingConvention.Cdecl)]
    private static extern double scale_raw(double x, double factor);
    public static double Scale(double x, double factor) => scale_raw(x, factor);

    // string arguments are marshalled as UTF-8
    [DllImport(Lib, EntryPoint = "greet", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
    private static extern IntPtr greet_raw([MarshalAs(UnmanagedType.LPUTF8Str)] string name);
    public static string Greet(string name)
    {
        var p = greet_raw(name);
        try { return Marshal.PtrToStringUTF8(p)!; } finally { FreeString(p); }
    }

    [DllImport(Lib, EntryPoint = "free_string", CallingConvention = CallingConvention.Cdecl)]
    private static extern void free_string_raw(IntPtr s);
    private static void FreeString(IntPtr p) => free_string_raw(p);
}

Package contents

MathLib.0.1.0.nupkg
├─ MathLib.nuspec
├─ contentFiles/cs/any/Native.cs      (the generated wrapper, compiled into the consumer)
├─ runtimes/win-x64/native/mathlib.dll
└─ [Content_Types].xml

In a C# project

dotnet add package MathLib --version 0.1.0 --source ./nupkg

Console.WriteLine(Native.Add(2, 3)); // 5

Try it: edit the Rust functions and watch the C# wrapper follow, add a platform, then try “Use NuGet in Rust”.

Share Rust with your .NET team

Rust is a good place for the code that must be fast, safe and portable, and .NET is a good place for the application around it. Packing a Rust library as a NuGet package lets a C# project call it like any other library: dotnet add package, then Native.Add(2, 3).

The package carries the generated wrapper as source that compiles into the consuming project, and the native library under runtimes/<platform>/native, which is where .NET looks for it. We verified it end to end with a real C# program restoring the package and calling the Rust code.

  • Supports the .NET SDK package flow: restore, build, run
  • Includes every platform you have built under target/<triple>/release
  • Functions that cannot cross the boundary are skipped with a reason

Use NuGet packages from Rust

Many native SDKs ship on NuGet. Ruststudio restores them into the solution, gives your builds a NUGET_<ID>_DIR variable, and Use in project writes a block into build.rs that adds the package's native folder to the linker path and copies its libraries next to your program.

This covers packages that carry native libraries, including Rust libraries packed by Ruststudio. Managed .NET libraries are not hosted inside Rust programs.

Frequently asked questions

Something else? Ask in the community forum or send feedback.

Does the consuming .NET project need Rust?

No. The package contains the compiled native library. The .NET project only needs the package.

Which platforms are included?

The platform you build on, plus any target you have built under target/<triple>/release before packing.

Can I call any .NET package from Rust?

Native packages, yes. Managed .NET libraries are not hosted inside Rust programs by Ruststudio.

Is Ruststudio free?

Yes. Ruststudio is free for personal and commercial use, with no licence key and nothing to pay. Use it for 7 days straight away; after that you sign up for a free account with your e-mail address. It is closed-source freeware.

Who makes Ruststudio?

Ruststudio is a product of FOWLGUARD, Inc., the company behind Fowlguard Secure and Fowlguard Productivity. It is one of its free tools; see fowlguard.com.

Which systems does Ruststudio run on?

Windows 10 and 11 (64-bit), macOS 11 or later (Apple silicon and Intel) and Linux (x86-64, X11 or Wayland). You can build for the desktop, the web and Android from it. Native iOS support is in development and will need a Mac with Xcode.