Complete the code to print a message about Rust's safety.
fn main() {
println!("Rust is {} and fast.");
}Rust is known for being safe and fast, which helps prevent bugs.
Complete the code to declare a variable that cannot be changed in Rust.
fn main() {
let [1] = 10;
// This variable cannot be changed
}In Rust, variables are immutable by default. Just 'let x' means it cannot be changed.
Fix the error in the Rust code that tries to change an immutable variable.
fn main() {
let mut x = 5;
x = [1];
println!("x is {}", x);
}The error is trying to assign a new value to an immutable variable. To fix, declare 'x' as mutable or avoid reassignment. Here, just assigning a value to 'x' is the fix.
Fill both blanks to create a Rust function that returns the sum of two numbers.
fn sum(a: i32, b: i32) -> i32 { [1] a + b; } fn main() { let result = sum(5, 7); println!("Sum is: {}", [2]); }
The function uses 'return' to send back the sum. Then 'result' holds the sum to print.
Fill all three blanks to create a Rust program that uses a vector and prints its length.
fn main() {
let mut numbers = Vec::new();
numbers.[1](10);
numbers.[2](20);
println!("Length is: {}", numbers.[3]());
}'push' adds elements to the vector. 'len' returns the number of elements.