Content deleted Content added
←Removed redirect to Rust (programming language)#Syntax and features Tags: Removed redirect harv or sfn error |
|||
(11 intermediate revisions by 6 users not shown) | |||
Line 1:
{{Short description|Set of rules defining correctly structured programs for the Rust programming language}}
[[File:Screen1 2.png|thumb|A snippet of Rust code]]
The '''syntax of Rust''' is [[syntax|the set of rules]] defining how a [[Rust (programming language)|Rust]] program is written and compiled.
Rust's [[Syntax (programming languages)|syntax]] is similar to that of [[C (programming language)|C]] and [[C++]],<ref>{{Cite news |last=Proven |first=Liam |date=2019-11-27 |title=Rebecca Rumbul named new CEO of The Rust Foundation |url=https://www.theregister.com/2021/11/19/rust_foundation_ceo/ |access-date=2022-07-14 |website=[[The Register]] |language=en |quote="Both are curly bracket languages, with C-like syntax that makes them unintimidating for C programmers." |archive-date=2022-07-14 |archive-url=https://web.archive.org/web/20220714110957/https://www.theregister.com/2021/11/19/rust_foundation_ceo/ |url-status=live }}</ref><ref name=":4">{{Cite web |last=Vigliarolo |first=Brandon |date=2021-02-10 |title=The Rust programming language now has its own independent foundation |url=https://www.techrepublic.com/article/the-rust-programming-language-now-has-its-own-independent-foundation/ |archive-url=https://web.archive.org/web/20230320172900/https://www.techrepublic.com/article/the-rust-programming-language-now-has-its-own-independent-foundation/ |archive-date=2023-03-20 |access-date=2022-07-14 |website=[[TechRepublic]] |language=en-US}}</ref> although many of its features were influenced by [[functional programming]] languages such as [[OCaml]].{{sfn|Klabnik|Nichols|2019|p=263}}
== Basics ==
Although Rust syntax is heavily influenced by the syntaxes of C and C++, the syntax of Rust is far more distinct from [[C++ syntax]] than
Below is a [["Hello, World!" program]] in Rust. The {{Rust|fn}} keyword denotes a [[Function (computer programming)|function]], and the {{Rust|println!}} [[Macro (computer science)|macro]] (see {{Section link|2=Macros|nopage=y}}) prints the message to [[standard output]].{{sfn|Klabnik|Nichols|2019|pp=5–6}} [[Statement (computer science)|Statements]] in Rust are separated by [[Semicolon#Programming|semicolons]].
Line 86 ⟶ 87:
<syntaxhighlight lang="rust">
fn main() {
let foo: i32 = 10;
println!("The value of foo is {foo}");
}
Line 96 ⟶ 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 107 ⟶ 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 120 ⟶ 121:
<syntaxhighlight lang="rust">
fn main() {
let letters: str = "abc";
let letters: usize = letters.len();
}
</syntaxhighlight>
Line 131 ⟶ 132:
<syntaxhighlight lang="rust">
fn main() {
let x: i32 = {
println!("this is inside the block");
1 + 2
Line 153 ⟶ 154:
<syntaxhighlight lang="rust">
fn main() {
let x: i32 = 10;
if x > 5 {
println!("value is greater than five");
Line 172 ⟶ 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 184 ⟶ 185:
fn main() {
// Iterate over all integers from 4 to 10
let mut value: i32 = 4;
while value <= 10 {
println!("value = {value}");
Line 212 ⟶ 213:
<syntaxhighlight lang="rust">
(1..=100).filter(|&x: i8| -> bool x % 3 == 0).sum()
</syntaxhighlight>
Line 221 ⟶ 222:
<syntaxhighlight lang="rust">
fn main() {
let value: i32 = 456;
let mut x: i32 = 1;
let y = loop {
x *= 10;
Line 231 ⟶ 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 278 ⟶ 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 295 ⟶ 298:
let tuple: (u32, i64) = (3, -3);
let array: [i8; 5] = [1, 2, 3, 4, 5];
let
let value: i8 =
</syntaxhighlight>
Line 307 ⟶ 309:
<!-- todo str, and ! -->
== Ownership and references ==
Line 317 ⟶ 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 332 ⟶ 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 351 ⟶ 354:
<syntaxhighlight lang="rust">
fn main() {
let x: i32 = 5;
// |
let r: &i32 = &x;
// | |
println!("r: {}", r); // | |
Line 365 ⟶ 368:
<syntaxhighlight lang="rust">
fn main() {
let r: &i32;
// |
{ // |
let x: i32 = 5;
r = &x; // ERROR: x does | |
} // not live long -| |
Line 638 ⟶ 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 706 ⟶ 709:
fn main() {
let x: i32 = sum!(1, 2, 3);
println!("{x}"); // prints 6
}
Line 727 ⟶ 730:
* [[C++ syntax]]
* [[Java syntax]]
* [[C Sharp syntax|C# syntax]]
== Notes ==
Line 737 ⟶ 740:
<ref name="influences">{{cite web |title=Influences |url=https://doc.rust-lang.org/reference/influences.html |website=The Rust Reference |access-date=December 31, 2023 |archive-date=November 26, 2023 |archive-url=https://web.archive.org/web/20231126231034/https://doc.rust-lang.org/reference/influences.html |url-status=live}}</ref>
}}
== Sources ==
{{refbegin}}
* {{Cite book |last=Gjengset |first=Jon |title=Rust for Rustaceans |date=2021 |publisher=No Starch Press |isbn=9781718501850 |edition=1st |oclc=1277511986 |language=en}}
* {{Cite book|last1=Klabnik|first1=Steve|url=https://books.google.com/books?id=0Vv6DwAAQBAJ|title=The Rust Programming Language (Covers Rust 2018)|last2=Nichols|first2=Carol|date=2019-08-12|publisher=No Starch Press|isbn=978-1-7185-0044-0|language=en}}
* {{Cite book |last1=Klabnik |first1=Steve |last2=Nichols |first2=Carol |title=The Rust programming language |date=2023 |publisher=No Starch Press |isbn=978-1-7185-0310-6 |edition=2nd |oclc=1363816350}}
* {{Cite book|last1=McNamara|first1=Tim|title=Rust in Action|oclc=1153044639|date=2021|publisher=Manning Publications|isbn=978-1-6172-9455-6|language=en}}
{{refend}}
[[Category:Rust (programming language)]]
[[Category:Programming language syntax]]
|