Back to Blog
Rust language book7/2/2023 ![]() ![]() The way it does this is by calling drop automatically at the closing curly bracket where your value goes out of scope. Here! A gift for you-it’s the memory that is no longer in use. The compiler does the work for you, no garbage collection needed! Instead of having to keep track of what memory you use throughout your program, Rust returns the memory after your variable goes out of scope. That’s all I’ll say here for now.įurther reading: What’s the difference? - String::from() vs “literal” Rust won’t let your memory go to waste The key here is that "hello" is a string literal. It takes in a string literal like "hello" and returns a String. String is a string type which has a function under its namespace called from(). ![]() I won’t go into depth on this because I could write an entire article on it, but look at this code: let s = String :: from ( "hello" ) ![]() As they say in the book,Īt this point, the relationship between scopes and when variables are valid is similar to that in other programming languages. Values are only valid within a certain range. In JavaScript, there are rules where variables are only accessible within specific scopes. But be aware that by dropping a value when it goes out of scope, we remove the ability to use or reference it. You can’t remove memory though.ĭropping seems to be the one the authors use so we will stick with that. I don’t want to hyper-focus because it isn’t useful. Removing can mean the same thing depending on how you use it. Dropping a value in the context of ownership means that the value is no longer guaranteed to be in memory. I asked myself this question, and I think the answer is it depends. is dropping a value the same as removing a value? This means that Rust can guarantee a value is never owned by more than one owner so that it can safely drop the value when it’s out of scope and free up that memory. When a value goes out of scope, Rust drops the value.They are only allowed one owner at a time.I would encourage you to scroll to the Glossary section at the bottom if you see an unfamiliar term. This feature - unique to Rust - allows the language to guarantee memory safety without using a garbage collector.Ĭoming from languages like JavaScript or Python, this might feel foreign to you. In this chapter, we learn all about ownership. Previously, I covered other chapters (most recently Chapter 3) and contributing to Rust open source. New to this edition: An extended section on Rust macros, an expanded chapter on modules, and appendixes on Rust development tools and editions.This is the next blog post in a series related to The Rust Programming Language Book. You'll find plenty of code examples throughout the book, as well as three chapters dedicated to building complete projects to test your learning: a number guessing game, a Rust implementation of a command line tool, and a multithreaded server. You'll begin with basics like creating functions, choosing data types, and binding variables and then move on to more advanced concepts, such as: Ownership and borrowing, lifetimes, and traits Using Rust's memory safety guarantees to build fast, safe programs Testing, error handling, and effective refactoring Generics, smart pointers, multithreading, trait objects, and advanced pattern matching Using Cargo, Rust's built-in package manager, to build, test, and document your code and manage dependencies How best to use Rust's advanced compiler with compiler-led programming techniques. ![]() The authors of The Rust Programming Language, members of the Rust Core Team, share their knowledge and experience to show you how to take full advantage of Rust's features - from installation to creating robust and scalable programs. Rust offers control over low-level details (such as memory usage) in combination with high-level ergonomics, eliminating the hassle traditionally associated with low-level languages. The Rust Programming Language is the official book on Rust: an open source systems programming language that helps you write faster, more reliable software. ![]()
0 Comments
Read More
Leave a Reply. |