<예제 코드_1>
[Python]
if __name__ == '__main__':
x = 1;
print(x);
# bind x again shadows old one from above
x = 'i';
print(x)
# declare, init
something = None;
x = 5;
print("x, something", x, something);
something = x * 5;
print("x, something", x, something);
# mutability
y = 0;
y = y * 2 + x;
print(y);
BLAH = 42; # only const by convention
y *= BLAH;
[Rust]
fn main() {
let x = 1;
println!("x: {}", x);
// binds x again, shadowning the old one from above
let x = 'i';
println!("x; {}", x);
// declare, initialize
let something;
let x = 5;
// println!("x, something: {}, {}", x, something); // Error
something = x * 5;
println!("x, something: {}, {}", x, something);
// Mutablity
let mut y = 0; // Need to declare 'mut'
y = y * 2 + x;
dbg!(y);
const BLAH: i32 = 42; // need to change Upper Capital. Don't know why.
y *= BLAH;
dbg!(y);
}
<예제 코드_2>
[Python]
# LEGB rule: Local, Enclosing, Global, and Built-in
global_variable = 1;
def f():
global global_variable;
local_variable = 2;
def subf():
print("local", local_variable);
print("global", global_variable);
enclosed_variable = 3;
# local_variable = 5;
subf();
# print("enclosed", enclosed_variable);
global_variable = 9;
local_variable = 1;
print("global", global_variable);
print("local", local_variable);
# example use of built-in scope
blah = int("42");
if __name__ == '__main__':
f();
[Rust]
fn main() {
// scope
// This binding lives in the main function
let long_lived_binding = 1;
// This is a block, and has a smaller scope than the main function
{
// This binding only exists in this block.
let short_lived_binding = 2;
println!("inner short: {}", short_lived_binding);
// This binding "shadows" the outer one
let long_lived_binding = 5_f32;
println!("inner long: {}", long_lived_binding);
}
// End of the block
// Error! `short_lived_binding` doesn't exist in this scope
// println!("outer short: {}", short_lived_binding);
println!("outer long: {}", long_lived_binding);
// This binding also *shadows* the previous binding
let long_lived_binding = 'a';
println!("outer long: {}", long_lived_binding);
}
<참조 1> https://www.youtube.com/watch?v=vlw0P8a40Oc
<참조_2>
'Rust' 카테고리의 다른 글
[Rust] Scraping : Naver 메인 페이지 예제 (60일차) (0) | 2023.02.20 |
---|---|
[Rust] Scraping 예제 (59일차) (0) | 2023.02.19 |
[Rust] Web Spider 예제 (57일차) (0) | 2023.02.12 |
[Rust] 실시간 Chat App in Rocket (56일차) (0) | 2023.02.09 |
댓글