Last active
November 17, 2024 18:09
-
-
Save dacr/cc2a879ab3fdc39e5957aa13f9b037c4 to your computer and use it in GitHub Desktop.
hello rust mutations / published by https://github.com/dacr/code-examples-manager #1a97069e-facd-4c5a-a52c-56dada02659e/f52e1d2e254a9a4fafe5830d681e2ca5079670f6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env rust-script | |
// summary : hello rust mutations | |
// keywords : rust, mutations, @testable | |
// publish : gist | |
// authors : David Crosson | |
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) | |
// id : 1a97069e-facd-4c5a-a52c-56dada02659e | |
// created-on : 2024-10-12T21:24:30+02:00 | |
// managed-by : https://github.com/dacr/code-examples-manager | |
// run-with : ./$file | |
fn main() { | |
// ------------------------------------- | |
{ | |
let immutable_by_default = 32; | |
let mut made_mutable = 42; | |
made_mutable = 24; | |
assert_eq!(immutable_by_default, 32); | |
assert_eq!(made_mutable, 24); | |
println!("{}", made_mutable); | |
} | |
// ------------------------------------- | |
{ | |
let mut hello = "hello".to_string(); | |
hello.push_str(" world"); // won't work without mut : | |
hello = "world".to_string(); // won't work without mut : | |
println!("`mut` mean both the reference AND the referenced content can be modified") | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment