Skip to content

Instantly share code, notes, and snippets.

@carols10cents
Last active November 5, 2024 09:30
Show Gist options
  • Save carols10cents/47f87b480e35f65897bf to your computer and use it in GitHub Desktop.
Save carols10cents/47f87b480e35f65897bf to your computer and use it in GitHub Desktop.
C# to Rust Cheat Sheet

Thanks to @seejee for making this for me!!!

C# to Rust Cheat Sheet

The goal of this is to have an easily-scannable reference for the most common syntax idioms in C# and Rust so that programmers most comfortable with C# can quickly get through the syntax differences and feel like they could read and write basic Rust programs.

What do you think? Does this meet its goal? If not, why not?

Variables

C#:

var foo = 1;
var bar = "hi";
var somethingThatVaries = 2;
somethingThatVaries += 1;

Rust:

let foo = 1i;
let bar = "hi";
let mut something_that_varies = 2;
something_that_varies += 1;

Functions

C#:

// Function definition: takes an integer argument, returns an integer
static int DoSomething(int some_argument)
{
  return some_argument + 1;
}

// Function use
var results = DoSomething(3);

Rust:

// Function definition: takes an integer argument, returns an integer
fn do_something(some_argument: int) -> int {
    some_argument + 1 // no semicolon in implicit return statements
}

// Function use
let results = do_something(3);

Conditionals

C#:

if (x == 3)
{
  // ...
}
else if (x == 0)
{
  // ...
}
else
{
  // ...
}
// Or using a switch:
switch(x) {
  case 3:
    // ...
    break;
  case 0:
    // ...
    break;
  default:
    // ...
    break;
}

Rust:

if x == 3 {
    // ...
} else if x == 0 {
    // ...
} else {
    // ...
}

More commonly used in Rust is pattern matching, which gives you other benefits:

match x {
    3 => {
        // ...
    },
    0 => {
        // ...
    },
    _ => {
        // ...
    }
}

Output to the screen

C#:

var x = 5;
System.Console.WriteLine("x has the value {0}", x);

Rust:

let x = 5;
println!("x has the value {}", x);

Lists of variable size

I'm not saying the word "array" on purpose :) This is how to do Arrays that can grow in size.

C#:

var i = new List<String>() { "a", "b", "c" };
i.Add("d");
Console.WriteLine(i[1]); // outputs b

Rust:

let i = vec!["a", "b", "c"];
i.push("d");
println!("{}", i[1]); // outputs b

Iterating over the elements in a list

C#:

var i = new List<String>() { "a", "b", "c" };

foreach(var j in i) {
  Console.WriteLine(j);
}

i.ForEach((j) => Console.WriteLine(j));

Rust:

let i = vec!["a", "b", "c"];
for j in i.iter() {
    println!("{}", j);
}

Tests

C#: (NUnit)

using NUnit.Framework;

[TestFixture]
public class SomeTestClass
 {
   [Test]
   public void SomeTest()
    {
      Assert.AreEqual(1, 1); //will pass
      Assert.AreEqual(1, 2); //will fail
    }
  }
}

Rust:

#[test]
fn some() {
    assert!(true); // will pass
    assert_eq!("expected", "actual"); // will fail
}

Encapsulation of data + behavior

I'm not saying the word "object" on purpose :)

C#:

class Highway {
  public int Length     {get; private set;}
  public int SpeedLimit {get; private set;}

  public Highway(int length, int speedLimit) {
    this.Length = length;
    this.SpeedLimit = speedLimit;
  }
  
  public int TimeToTravel() {
    return Length / SpeedLimit;
  }
}

new Highway(325, 65).TimeToTravel(); // returns 5

Rust:

struct Highway {
    length: int,
    speed_limit: int,
}

impl Highway {
    fn time_to_travel(&self) -> int {
        self.length / self.speed_limit
    }
}

Highway { length: 325, speed_limit: 65 }.time_to_travel() // returns 5
@danielleiszen
Copy link

The last example could be written in C# as

class Highway {
    public int Length = 0;
    public int SpeedLimit = 0;
    
    public int TimeToTravel() {
        return Length / SpeedLimit;
    }
}
new Highway{Length = 325, SpeedLimit = 65}.TimeToTravel()

Not true, the rust version is inmutable, this c# code is mutable even from outside of the class. However in the original example the properties also have private setters which allow the instance to mutate its state which is not allowed in the rust code. So both examples are wrong.

@lucian-asterx
Copy link

Then maybe it should be equivalent with using record type in C#, since in rust it is immutable and is a struct

The last example could be written in C# as

class Highway {
    public int Length = 0;
    public int SpeedLimit = 0;
    
    public int TimeToTravel() {
        return Length / SpeedLimit;
    }
}
new Highway{Length = 325, SpeedLimit = 65}.TimeToTravel()

Not true, the rust version is inmutable, this c# code is mutable even from outside of the class. However in the original example the properties also have private setters which allow the instance to mutate its state which is not allowed in the rust code. So both examples are wrong.

Then maybe it should be equivalent with using record type in C#, since in rust it is immutable and is a struct

@danielleiszen
Copy link

danielleiszen commented Sep 23, 2024

Then maybe it should be equivalent with using record type in C#, since in rust it is immutable and is a struct

There are initialization-only properties, that fit the situation I think.

class Highway {
  public int Length     {get; init;}
  public int SpeedLimit {get; init;}

  // constructor is not needed since initialization can be used
  
  public int TimeToTravel() {
    return Length / SpeedLimit;
  }
}

var hw = new Highway{Length = 325, SpeedLimit = 65};
hw.TimeToTravel(); // returns 5

This way the instance is immutable, because the initialization only properties cannot be set afterwards.

@lucian-asterx
Copy link

Then maybe it should be equivalent with using record type in C#, since in rust it is immutable and is a struct

There are initialization-only properties, that fit the situation I think.

class Highway {
  public int Length     {get; init;}
  public int SpeedLimit {get; init;}

  // constructor is not needed since initialization can be used
  
  public int TimeToTravel() {
    return Length / SpeedLimit;
  }
}

var hw = new Highway{Length = 325, SpeedLimit = 65};
hw.TimeToTravel(); // returns 5

This way the instance is immutable, because the initialization only properties cannot be set afterwards.

Does Rust deal the same way with structs like C# in terms of memory allocation? Stack vs Heap?
Because if Rust is allocating only memory on the Stack for the struct type, then the equivalent in C# cannot be a class because that allocates memory on the Heap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment