Skip to content

Update to Rust 2024 edition. #2658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,9 @@ jobs:
with:
fetch-depth: 0

- name: Update Rust
run: rustup update

- name: Setup Rust cache
uses: ./.github/workflows/setup-rust-cache

Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
with:
fetch-depth: 0 # We need the full history for build.sh below.

- name: Update Rust
run: rustup update

- name: Setup Rust cache
uses: ./.github/workflows/setup-rust-cache

Expand Down
2 changes: 1 addition & 1 deletion book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ src = "src"
title = "Comprehensive Rust 🦀"

[rust]
edition = "2021"
edition = "2024"

[build]
extra-watch-dirs = ["po", "third_party"]
Expand Down
2 changes: 1 addition & 1 deletion dprint.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"command": "yapf3",
"exts": ["py"]
}, {
"command": "rustup run stable rustfmt --edition 2021",
"command": "rustup run stable rustfmt --edition 2024",
"exts": ["rs"]
}]
},
Expand Down
2 changes: 1 addition & 1 deletion mdbook-course/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mdbook-course"
version = "0.1.0"
authors = ["Dustin Mitchell <[email protected]>"]
edition = "2021"
edition = "2024"
license = "Apache-2.0"
publish = false
repository = "https://github.com/google/comprehensive-rust"
Expand Down
14 changes: 6 additions & 8 deletions mdbook-course/src/course.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
//! item becomes the first slide in that segment. Any other sub-items of the
//! top-level item are treated as further slides in the same segment.

use crate::frontmatter::{split_frontmatter, Frontmatter};
use crate::markdown::{duration, Table};
use crate::frontmatter::{Frontmatter, split_frontmatter};
use crate::markdown::{Table, duration};
use mdbook::book::{Book, BookItem, Chapter};
use std::fmt::Write;
use std::path::PathBuf;
Expand Down Expand Up @@ -318,7 +318,9 @@ impl Session {
}
format!(
"Including {BREAK_DURATION} minute breaks, this session should take about {}. It contains:\n\n{}",
duration(self.minutes()), segments)
duration(self.minutes()),
segments
)
}

/// Return the total duration of this session.
Expand All @@ -337,11 +339,7 @@ impl Session {
///
/// This includes breaks between segments.
pub fn target_minutes(&self) -> u64 {
if self.target_minutes > 0 {
self.target_minutes
} else {
self.minutes()
}
if self.target_minutes > 0 { self.target_minutes } else { self.minutes() }
}
}

Expand Down
2 changes: 1 addition & 1 deletion mdbook-exerciser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mdbook-exerciser"
version = "0.1.0"
authors = ["Andrew Walbran <[email protected]>"]
edition = "2021"
edition = "2024"
license = "Apache-2.0"
repository = "https://github.com/google/comprehensive-rust"
description = "A tool for extracting starter code for exercises from Markdown files."
Expand Down
2 changes: 1 addition & 1 deletion mdbook-exerciser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use log::{info, trace};
use pulldown_cmark::{Event, Parser, Tag, TagEnd};
use std::fs::{create_dir_all, File};
use std::fs::{File, create_dir_all};
use std::io::Write;
use std::path::Path;

Expand Down
2 changes: 1 addition & 1 deletion mdbook-exerciser/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

use anyhow::Context;
use log::trace;
use mdbook::BookItem;
use mdbook::book::Book;
use mdbook::renderer::RenderContext;
use mdbook::BookItem;
use mdbook_exerciser::process;
use std::fs::{create_dir, remove_dir_all};
use std::io::stdin;
Expand Down
2 changes: 1 addition & 1 deletion src/android/interoperability/java/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
// ANCHOR: hello
//! Rust <-> Java FFI demo.

use jni::JNIEnv;
use jni::objects::{JClass, JString};
use jni::sys::jstring;
use jni::JNIEnv;

/// HelloWorld::hello method implementation.
// SAFETY: There is no other global function of this name.
Expand Down
2 changes: 1 addition & 1 deletion src/android/testing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "android-testing"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[[example]]
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/alloc-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[package]
name = "alloc-example"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
5 changes: 3 additions & 2 deletions src/bare-metal/alloc-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@ use buddy_system_allocator::LockedHeap;
#[global_allocator]
static HEAP_ALLOCATOR: LockedHeap<32> = LockedHeap::<32>::new();

static mut HEAP: [u8; 65536] = [0; 65536];
const HEAP_SIZE: usize = 65536;
static mut HEAP: [u8; HEAP_SIZE] = [0; HEAP_SIZE];

pub fn entry() {
// SAFETY: `HEAP` is only used here and `entry` is only called once.
unsafe {
// Give the allocator some memory to allocate.
HEAP_ALLOCATOR.lock().init(HEAP.as_mut_ptr() as usize, HEAP.len());
HEAP_ALLOCATOR.lock().init(&raw mut HEAP as usize, HEAP_SIZE);
}

// Now we can do things that require heap allocation.
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/aps/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[package]
name = "ap-examples"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
7 changes: 5 additions & 2 deletions src/bare-metal/aps/examples/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@ use cc::Build;
use std::env;

fn main() {
env::set_var("CROSS_COMPILE", "aarch64-none-elf");
env::set_var("CC", "clang");
// SAFETY: The build script is single-threaded.
unsafe {
env::set_var("CROSS_COMPILE", "aarch64-none-elf");
env::set_var("CC", "clang");
}

Build::new()
.file("entry.S")
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/aps/examples/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// ANCHOR: exceptions
use log::error;
use smccc::psci::system_off;
use smccc::Hvc;
use smccc::psci::system_off;

// SAFETY: There is no other global function of this name.
#[unsafe(no_mangle)]
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/aps/examples/src/main_improved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::pl011::Uart;
use core::fmt::Write;
use core::panic::PanicInfo;
use log::error;
use smccc::psci::system_off;
use smccc::Hvc;
use smccc::psci::system_off;

/// Base address of the primary PL011 UART.
const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _;
Expand Down
4 changes: 2 additions & 2 deletions src/bare-metal/aps/examples/src/main_logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ mod pl011;

use crate::pl011::Uart;
use core::panic::PanicInfo;
use log::{error, info, LevelFilter};
use smccc::psci::system_off;
use log::{LevelFilter, error, info};
use smccc::Hvc;
use smccc::psci::system_off;

/// Base address of the primary PL011 UART.
const PL011_BASE_ADDRESS: *mut u32 = 0x900_0000 as _;
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/aps/examples/src/main_minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::pl011_minimal::Uart;
use core::fmt::Write;
use core::panic::PanicInfo;
use log::error;
use smccc::psci::system_off;
use smccc::Hvc;
use smccc::psci::system_off;

/// Base address of the primary PL011 UART.
const PL011_BASE_ADDRESS: *mut u8 = 0x900_0000 as _;
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/microcontrollers/examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[package]
name = "microcontroller-examples"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/microcontrollers/examples/src/bin/hal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ extern crate panic_halt as _;

use cortex_m_rt::entry;
use embedded_hal::digital::OutputPin;
use nrf52833_hal::gpio::{p0, Level};
use nrf52833_hal::gpio::{Level, p0};
use nrf52833_hal::pac::Peripherals;

#[entry]
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/useful-crates/allocator-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "allocator-example"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/useful-crates/tinyvec.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ many elements are used and panics if you try to use more than are allocated.
<!-- mdbook-xgettext: skip -->

```rust,editable,compile_fail
use tinyvec::{array_vec, ArrayVec};
use tinyvec::{ArrayVec, array_vec};

fn main() {
let mut numbers: ArrayVec<[u32; 5]> = array_vec!(42, 66);
Expand Down
2 changes: 1 addition & 1 deletion src/bare-metal/useful-crates/zerocopy-example/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "zerocopy-example"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion src/borrowing/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "borrowing"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[lib]
Expand Down
4 changes: 2 additions & 2 deletions src/cargo/rust-ecosystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ Key points:
- Dependencies can also be resolved from alternative [registries], git, folders,
and more.

- Rust also has [editions]: the current edition is Rust 2021. Previous editions
were Rust 2015 and Rust 2018.
- Rust also has [editions]: the current edition is Rust 2024. Previous editions
were Rust 2015, Rust 2018 and Rust 2021.

- The editions are allowed to make backwards incompatible changes to the
language.
Expand Down
6 changes: 1 addition & 5 deletions src/closures/capturing.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@ A closure can capture variables from the environment where it was defined.
fn main() {
let max_value = 5;
let clamp = |v| {
if v > max_value {
max_value
} else {
v
}
if v > max_value { max_value } else { v }
};

dbg!(clamp(1));
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/async-control-flow/select.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ the resulting variables. The `statement` result becomes the result of the

```rust,editable,compile_fail
use tokio::sync::mpsc;
use tokio::time::{sleep, Duration};
use tokio::time::{Duration, sleep};

#[tokio::main]
async fn main() {
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/async-exercises/chat-async/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "chat-async"
version = "0.1.0"
edition = "2021"
edition = "2024"

[dependencies]
futures-util = { version = "0.3.31", features = ["sink"] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

// ANCHOR: solution
// ANCHOR: setup
use futures_util::stream::StreamExt;
use futures_util::SinkExt;
use futures_util::stream::StreamExt;
use http::Uri;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio_websockets::{ClientBuilder, Message};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use futures_util::stream::StreamExt;
use std::error::Error;
use std::net::SocketAddr;
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::broadcast::{channel, Sender};
use tokio::sync::broadcast::{Sender, channel};
use tokio_websockets::{Message, ServerBuilder, WebSocketStream};
// ANCHOR_END: setup

Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/async-exercises/dining-philosophers.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ can use the following `Cargo.toml`:
[package]
name = "dining-philosophers-async-dine"
version = "0.1.0"
edition = "2021"
edition = "2024"

[dependencies]
tokio = { version = "1.26.0", features = ["sync", "time", "macros", "rt-multi-thread"] }
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/async-exercises/dining-philosophers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// ANCHOR: solution
// ANCHOR: Philosopher
use std::sync::Arc;
use tokio::sync::{mpsc, Mutex};
use tokio::sync::{Mutex, mpsc};
use tokio::time;

struct Chopstick;
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/async-pitfalls/async-traits.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ with some caveats:
```rust,editable,compile_fail
use async_trait::async_trait;
use std::time::Instant;
use tokio::time::{sleep, Duration};
use tokio::time::{Duration, sleep};

#[async_trait]
trait Sleeper {
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/async-pitfalls/pin.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ location.
```rust,editable,compile_fail
use tokio::sync::{mpsc, oneshot};
use tokio::task::spawn;
use tokio::time::{sleep, Duration};
use tokio::time::{Duration, sleep};

// A work item. In this case, just sleep for the given time and respond
// with a message on the `respond_on` channel.
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/sync-exercises/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "sync-exercises"
version = "0.1.0"
edition = "2021"
edition = "2024"
publish = false

[[bin]]
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/sync-exercises/dining-philosophers.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ You can use the following `Cargo.toml`:
[package]
name = "dining-philosophers"
version = "0.1.0"
edition = "2021"
edition = "2024"
```

<details>
Expand Down
2 changes: 1 addition & 1 deletion src/concurrency/sync-exercises/dining-philosophers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

// ANCHOR: solution
// ANCHOR: Philosopher
use std::sync::{mpsc, Arc, Mutex};
use std::sync::{Arc, Mutex, mpsc};
use std::thread;
use std::time::Duration;

Expand Down
Loading
Loading