Commentaries

Chapter 3. Fundamental Types

To a great extent, the Rust language is designed around its types. Its support for high- performance code arises from letting developers choose the data representation that best fits the situation, with the right balance between simplicity and cost. Rust’s mem‐ ory and thread safety guarantees also rest on the soundness of its type system, and Rust’s flexibility stems from its generic types and traits.

Compared to a dynamically typed language like JavaScript or Python, Rust requires more planning from you up front. You must spell out the types of function arguments and return values, struct fields, and a few other constructs. However, two features of Rust make this less trouble than you might expect:

동적 타입인 자바스크립트와 파이썬과는 달리, 러스트는 처음부터 타입에 대한 계획을 필요로한다.

**fn** build_vector() -> Vec<**i16**> {
			**let mut** v: Vec<**i16**> = Vec::<**i16**>::new();
				v.push(10**i16**);
				v.push(20**i16**);
				v
				}

위와 같이 써 줄 필요가 없음.

fn build_vector() -> Vec<i16> {
	let mut  v = Vec::new();
	v.push(10);
	v.push(20);
	v
		}

위와 같은 정도면 충분함. 위의 두 코드는 정확히 같은 머신 코드를 만들어냄.

컴파일 타임에 타입 에러를 잡아냄.

함수는 generic할 수 있다. : 단일 함수는 많은 타입의 값에서 사용 될 수 있다.

In Python and JavaScript, all functions work this way naturally: a function can operate on any value that has the properties and methods the function will need. (This is the characteristic often called duck typing: if it quacks like a duck, it’s a duck.) But it’s exactly this flexibility that makes it so difficult for those languages to detect type errors early; testing is often the only way to catch such mistakes. Rust’s generic functions give the language a degree of the same flexibility, while still catching all type errors at compile time.