Rust syntax: Difference between revisions

Content deleted Content added
Citation bot (talk | contribs)
Removed URL that duplicated identifier. | Use this bot. Report bugs. | #UCB_CommandLine
 
(4 intermediate revisions by the same user not shown)
Line 87:
<syntaxhighlight lang="rust">
fn main() {
let foo: i32 = 10;
println!("The value of foo is {foo}");
}
Line 97:
fn main() {
// This code would not compile without adding "mut".
let mut foo: i32 = 10;
println!("The value of foo is {foo}");
foo = 20;
Line 108:
<syntaxhighlight lang="rust">
fn main() {
let foo: i32 = 10;
// This will output "The value of foo is 10"
println!("The value of foo is {foo}");
let foo: i32 = foo * 2;
// This will output "The value of foo is 20"
println!("The value of foo is {foo}");
Line 121:
<syntaxhighlight lang="rust">
fn main() {
let letters: str = "abc";
let letters: usize = letters.len();
}
</syntaxhighlight>
Line 132:
<syntaxhighlight lang="rust">
fn main() {
let x: i32 = {
println!("this is inside the block");
1 + 2
Line 154:
<syntaxhighlight lang="rust">
fn main() {
let x: i32 = 10;
if x > 5 {
println!("value is greater than five");
Line 173:
<syntaxhighlight lang="rust">
fn main() {
let x: i32 = 10;
let new_x: i32 = if x % 2 == 0 { x / 2 } else { 3 * x + 1 };
println!("{new_x}");
}
Line 185:
fn main() {
// Iterate over all integers from 4 to 10
let mut value: i32 = 4;
while value <= 10 {
println!("value = {value}");
Line 213:
 
<syntaxhighlight lang="rust">
(1..=100).filter(|&x: i8| -> bool x % 3 == 0).sum()
</syntaxhighlight>
 
Line 222:
<syntaxhighlight lang="rust">
fn main() {
let value: i32 = 456;
let mut x: i32 = 1;
let y = loop {
x *= 10;
Line 232:
println!("largest power of ten that is smaller than or equal to value: {y}");
 
let mut up: i32 = 1;
'outer: loop {
let mut down: i32 = 120;
loop {
if up > 100 {
Line 279:
== Types ==
Rust is [[strongly typed]] and [[statically typed]], meaning that the types of all variables must be known at compilation time. Assigning a value of a particular type to a differently typed variable causes a [[compilation error]]. [[Type inference]] is used to determine the type of variables if unspecified.{{sfn|Klabnik|Nichols|2019|pp=24}}
 
The type <code>()</code>, called the "unit type" in Rust, is a concrete type that has exactly one value. It occupies no memory (as it represents the absence of value). All functions that do not have an indicated return type implicitly return <code>()</code>. It is similar to {{cpp|void}} in other C-style languages, however {{cpp|void}} denotes the absence of a type and cannot have any value.
 
The default integer type is {{rust|i32}}, and the default [[floating point]] type is {{rust|f64}}. If the type of a [[Literal (computer programming)|literal]] number is not explicitly provided, it is either inferred from the context or the default type is used.{{sfn|Klabnik|Nichols|2019|pp=36–38}}
Line 296 ⟶ 298:
let tuple: (u32, i64) = (3, -3);
let array: [i8; 5] = [1, 2, 3, 4, 5];
let tuplevalue: (bool, bool) = (true,tuple.1; true);// -3
let value: i8 = tuple.1array[2]; // -3
let value = array[2]; // 3
</syntaxhighlight>
 
Line 308 ⟶ 309:
 
<!-- todo str, and ! -->
 
== Ownership and references ==
 
Line 318 ⟶ 320:
 
fn main() {
let s: String = String::from("Hello, World");
print_string(s); // s consumed by print_string
// s has been moved, so cannot be used any more
Line 333 ⟶ 335:
 
fn main() {
let s: String = String::from("Hello, World");
print_string(&s); // s borrowed by print_string
print_string(&s); // s has not been consumed; we can call the function many times
Line 352 ⟶ 354:
<syntaxhighlight lang="rust">
fn main() {
let x: i32 = 5; // ------------------+- Lifetime 'a
// |
let r: &i32 = &x; // -+-- Lifetime 'b |
// | |
println!("r: {}", r); // | |
Line 366 ⟶ 368:
<syntaxhighlight lang="rust">
fn main() {
let r: &i32; // ------------------+- Lifetime 'a
// |
{ // |
let x: i32 = 5; // -+-- Lifetime 'b |
r = &x; // ERROR: x does | |
} // not live long -| |
Line 639 ⟶ 641:
 
fn main() {
let result1: i32 = sum(10, 20);
println!("Sum is: {}", result1); // Sum is: 30
 
let result2: f32 = sum(10.23, 20.45);
println!("Sum is: {}", result2); // Sum is: 30.68
}
Line 707 ⟶ 709:
 
fn main() {
let x: i32 = sum!(1, 2, 3);
println!("{x}"); // prints 6
}