Member-only story
Rust Strings compared to Ruby Strings
Let’s learn about Rust strings. As in plural. There are essentially two types. Um, what? You can’t be serious? Well, kinda.
Is this going to be one of those examples that demonstrates why Rust is hard to learn? No, not really. But, as simple as Strings are in other languages, they are a great gateway into the Rust ownership model, stack vs heap, etc… Before we get ahead of ourselves, let’s see some simple examples compared to Ruby.
In Ruby, we can use Strings as values for variables and constants. We’re going to use our Ruby project from the Getting Started post.
# lib/my_project_ruby.rb
module MyProjectRuby
class Error < StandardError; end
# Your code goes here...
puts "hello world"
end
You can see we already use a String in the puts
statement. The code “hello world” defines a String value. Let’s add some more examples.
# lib/my_project_ruby.rb
module MyProjectRuby
class Error < StandardError; end
NAME = "foo"
puts NAME.class # => String
substring = "this is a string".split(" ").last
puts substring.class # => String
puts "hello world".class
end
If you run this file via ruby lib/my_project_ruby.rb
from the project root, you will see:
String
String
String