Getting Started with Rust: Working with Files and Doing File I/O

How to develop command-line utilities in Rust.

This article demonstrates how to perform basic file and file
I/O operations in Rust, and also introduces Rust’s ownership concept and the Cargo tool.
If you are seeing Rust code for the first time, this article should provide
a pretty good idea of how Rust deals with files and file I/O, and if you’ve
used Rust before, you still will appreciate the code
examples in this article.

Ownership

It would be unfair to start talking about Rust without first discussing
ownership. Ownership is the Rust way of the developer having
control over the lifetime of a variable and the language in order to be safe.
Ownership means that the passing of a variable also passes the ownership
of the value to the new variable.

Another Rust feature
related to ownership is borrowing. Borrowing is about
taking control over a variable for a while and then returning that
ownership of the variable back. Although borrowing allows you to have
multiple references to a variable, only one reference can be mutable
at any given time.

Instead of continuing to talk theoretically about ownership and
borrowing, let’s look at a code example
called ownership.rs:


fn main() {
    // Part 1
    let integer = 321;
    let mut _my_integer = integer;
    println!("integer is {}", integer);
    println!("_my_integer is {}", _my_integer);
    _my_integer = 124;
    println!("_my_integer is {}", _my_integer);

    // Part 2
    let a_vector = vec![1, 2, 3, 4, 5];
    let ref _a_correct_vector = a_vector;
    println!("_a_correct_vector is {:?}", _a_correct_vector);

    // Part 3
    let mut a_var = 3.14;
    {
        let b_var = &mut a_var;
        *b_var = 3.14159;
    }
    println!("a_var is now {}", a_var);
}

So, what’s happening here? In the first part, you define an integer
variable (integer) and create a mutable variable based on
integer. Rust performs a full copy for primitive data types because
they are cheaper, so in this case, the integer and
_my_integer variables
are independent from each other.

However, for other types, such as a vector, you aren’t allowed to change
a variable after you have assigned it to another variable.
Additionally, you should use a reference for the
_a_correct_vector
variable of Part 2 in the above example, because Rust won’t make a copy of
a_vector.

Powered by WPeMatico