Systems in Rust
let formulation to create new variables:y:error[E0384]: cannot assign twice to immutable variable `s`
--> src/main.rs:3:4
|
2 | let s = "Imma string in RUST!"; // Rust comment
| - first assignment to `s`
3 | s = "Bleeblarbu";
| ^^^^^^^^^^^^^^^^ cannot assign twice to immutable variable
|>>> x = (1,2)
>>> x[1] = 2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignmentconst rather than let for all variables.script.js
node or in your browser.let mut formulation.
let mut :: .js let :: .py []let :: .js const :: .py ()src/main.rs
letlet, you should consider changing your code design equally as strongly as you consider adding mutsrc/main.rs and just furnish let to make the variable declarations well formed:int which is of theoretically infinite size and JavaScript, which only has floats.| Length | Signed | Unsigned |
|---|---|---|
| 8-bit | i8 |
u8 |
| 16-bit | i16 |
u16 |
| 32-bit | i32 |
u32 |
| 64-bit | i64 |
u64 |
| 128-bit | i128 |
u128 |
| architecture dependent | isize |
usize |
-1 for error handling does not apply to Rust for reasons we’ll cover latter, so always try to get code working with unsigned first.1 to these?
bool (like Python) and stylized all-lowercase (like JavaScript)''error[E0308]: mismatched types
--> src/main.rs:3:20
|
3 | let c : char = "a";
| ---- ^^^ expected `char`, found `&str`
| |
| expected due to this
|
help: if you meant to write a `char` literal, use single quotes
|
3 - let c : char = "a";
3 + let c : char = 'a';
|
For more information about this error, try `rustc --explain E0308`.
error: could not compile `scratch` (bin "scratch") due to 1 previous error
user@DESKTOP-THMS2PJ:~/tmp/scratch$ cat src/main.rs
fn main() {
let c : char = 'a';
let c : char = "a";
}&str// String of length 0
let s = "";
// Complete text of Bhagavad Gita
let t = "Dhritarashtra said: O Sanjay, after gathering on the holy field of Kurukshetra, and desiring to fight, what did my sons and the sons of Pandu do? ... { // s is not valid here, since it's not yet declared
let s = "hello"; // s is valid from this point forward
// do stuff with s
} // this scope is now over, and s is no longer valids exists at some time points, but not others.String, which is closer to data structure than a data type in some ways: let mut s = String::from("hello");
s.push_str(", world!"); // push_str() appends a literal to a String
println!("{s}"); // this will print `hello, world!`String is mutable, a literal is not.u64, our most beloved type, the ignomious String is fickle and fleeting.s passes “out of scope” as soon as it is assigned to t.s.String::from("6") isn’t a fixed width string of lenth 1 (to me at least).Whenever I use Rust, I just always
.cloneand when someone asks me about it, I say that’s a performance optimization for latter.
.clone on capital S String for e.g. Wordle, as needed..clone (you don’t need it)String operations into functions.
.clone() a capital S String beforing yeeting it into a helper.colour.rs
fn print_red(s:String) {
// Some terminal hacking nonsense for colors
println!("\u{001b}[31m{s}\u{001b}[0m");
}
fn print_grn(s:String) {
// More nonsense but 31 -> 32
println!("\u{001b}[32m{s}\u{001b}[0m");
}
fn main() {
let s = String::from("6");
print_red(s.clone());
print_grn(s.clone());
println!("{s}")
}6
6
6
colour.rs
loopforwhileif
else if instead of elifloop is its infinite loop type.loop, please use:
loop, withbreak! rather than not is logical negation.
!(x > 10) vs. not (x > 10)while
for loop btw.for is Pythonic “for each” rather than C/C++/Java/JavaScript “for” which should help you. for i in 0..5 { // Rust range
// Rust strings lack indices
// Instead they return either a character or a "None"
// We have to unwrap that
// Rust strings, amirite
println!("{}", guess.chars().nth(i).unwrap());
}cargo run told me how to write that.