Rust and .NET, both directions

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.

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

Try NuGet for Rust and .NET

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

Rust Studio · NuGetInteractive · simulated

src/lib.rs (edit me)

Platforms already built

Native.cs (generated: 4 functions)

// <auto-generated> by Rust Studio 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”.

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.

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. Rust Studio 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 Rust Studio. Managed .NET libraries are not hosted inside Rust programs.

Frequently asked questions

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 Rust Studio.

Is Rust Studio free?

Yes. Rust Studio is free for personal and commercial use, with no account, trial or licence key. It is closed-source freeware.

Which systems does Rust Studio run on?

Windows 10 and 11 (64-bit). You can build for Windows, the web and Android from it. iOS builds need a Mac with Xcode.