Systems in Rust
ixOwnership is a set of rules that govern how a Rust program manages memory.
String type is not a value, but a data structure.
take_ownership(s1) is called, the main ceases to be the “owner” of s1
s1 is “moved” to take_ownerships1 is considered invalid by the compiler.
take_ownership finishes, the memory owned by s is automatically cleaned up (dropped).
x, an i32, is a value, not a data structure.
make_copy(x) is called, a bit-for-bit copy of the value \(5\) is created and assigned to the parameter i.
x requires a (1) known and (2) finite number of bits.x remains valid and can be used after the function call.
src/main.rs
// src/main.rs (Snippet 3)
fn main() {
let s = String::from("hello world");
// Pass a reference (&s), which is borrowing.
let len = calculate_length(&s);
// 's' is still valid because ownership was not moved.
println!("'{}' has length {}", s, len);
}
fn calculate_length(s: &String) -> usize {
s.len()
// 's' (the reference) goes out of scope, but the String
// it points to is NOT dropped.
}&String type represents a reference to a String. This is an act of borrowing.
calculate_length can read the data via the reference s but does not take ownership.
s variable in main, it remains valid and can be used after the function call
i32 are copied when assigned/passed..0x3000 are hex memory locations.0x3004 and ends at 0x299CPay Attention
Do not click this while streaming.
String), which are too large for simple stack copying.
s is not the String data structure, but rather a description of where to find the data structure.t = s:
ts will trigger a compiler error."POPPY" live.
rustc) managed, suitable for small, short-lived data.0x3000) memory addressesx) variable names.
Oh yeah, the part of hardware that makes numerical (OS) to hardware (physical location) resolution fast.
structNow, run this program using cargo run:
$ cargo run
Compiling rect v0.1.0 (/home/user/tmp/rect)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.57s
Running `target/debug/rect`
The area of the rectangle is 1500 square pixels.This code succeeds in figuring out the area of the rectangle by calling the area function with each dimension, but we can do more to make this code clear and readable.
area function is supposed to calculate the area of one rectangle, but
struct is a design pattern, the negation of an anti-pattern.
tuplestructstruct with astruct is better in every way.$ cargo run
Compiling rect v0.1.0 (/home/user/tmp/rect)
error[E0277]: `Rectangle` doesn't implement `std::fmt::Display`
--> src/main.rs:12:24
|
12 | println!("rect1 is {rect1}");
| ^^^^^^^ `Rectangle` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `Rectangle`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
For more information about this error, try `rustc --explain E0277`.
error: could not compile `rect` (bin "rect") due to 1 previous error2>&1 takes errors (‘2’, for short), moves them (>) to output (1 for short).
>&) rather than overwrites.| takes the output of a command (cargo run 2>&1) and makes it the input of the next command.wc is wordcount - it counts lines, words, characters.$(()) is “arithmetic expansion”dbg!() or `println!(“{:?}”), and#[derive(Debug)]$ cargo run
Compiling rect v0.1.0 (/home/user/tmp/rect)
warning: fields `width` and `height` are never read
--> src/main.rs:3:5
|
2 | struct Rectangle {
| --------- fields in this struct
3 | width: u32,
| ^^^^^
4 | height: u32,
| ^^^^^^
|
= note: `Rectangle` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
warning: `rect` (bin "rect") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.37s
Running `target/debug/rect`
[src/main.rs:8:5] Rectangle { width: 30, height: 50, } = Rectangle {
width: 30,
height: 50,
}src/main.rs
dbg! yoinks ownership (cringe).
Comments