Rust and Nim are very different, especially when it comes to memory management.
You could convert Rust code to Nim, but idomatic Rust is unlikely to be
idiomatic Nim, due to several differences:
* **Memory**: Rust makes stack allocation with aliasing pretty easy, once one
understands lifetime parameters. In Nim most shared structures will get
allocated on the heap and GC'd. Already, there is a big paradigm shift:
++
struct NeedsStringRef<'a> {
reference: &'a String,
}
impl<'a> NeedsStringRef<'a> {
fn new(reference: &'a String) -> NeedsStringRef<'a> {
NeedsStringRef {
reference
}
}
}
fn main() {
let my_string = String::from("Hi!");
let uses_ref = NeedsStringRef::new(&my_string);
}
type
NeedsStringRef = object
reference: string
proc newNeedsStringRef(str: string): NeedsStringRef =
result = NeedsStringRef(reference: str)
let s = "Hi!"
let ns = newNeedsStringRef(s)
* **Typing**: Rust uses traits, a typeclassing system to provide interfaces
(constrain generics). Nim, by contrast, uses "class-like" single inheritance
and mixins. Some things here just don't transfer well.
* **Destruction**: Nim uses `defer`, GC finalizers, and explicit cleanup
(destructors aren't 100% usable yet). Rust uses RAII.
These differences are enough to make coding the same algorithm in the two
languages similar, but not similar _and idiomatic_.